Copy Code code as follows:
#-*-coding:utf8-*-
'''
Examples of Python common file operations
Path name Access function in Os.path module
Separated
basename () Remove directory path, return filename
DirName () Remove filename, return directory path
Join () combines the separated parts into a path name
Split () return (DirName (), basename ()) tuple
Splitdrive () return (drivename, pathname) tuple
Splitext () returns (filename, extension) tuple
Information
Getatime () returns the most recent access time
Getctime () return file creation time
Getmtime () returns the most recent file modification time
GetSize () returns the file size (in bytes)
Inquire
Exists () specifies whether the path (file or directory) exists
Isabs () Specifies whether the path is an absolute path
Isdir () Specifies whether the path exists and is a directory
Isfile () Specifies whether the path exists and is a file
Islink () Specifies whether the path exists and is a symbolic link
Ismount () Specifies whether the path exists and is a mount point
Samefile () Two path names pointing to the same file
Os.path.isdir (name): Determines if name is a directory, name is not a directory returns false
Os.path.isfile (name): Determines whether name is a file, does not exist name also returns false
Os.path.exists (name): Determines whether a file or directory name exists
Os.path.getsize (name): Get file size, if name is directory return 0L
Os.path.abspath (name): Get absolute path
Os.path.normpath (PATH): Canonical path string form
Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the filename, and it will not determine whether the file or directory exists)
Os.path.splitext (): Detach file name and extension
Os.path.join (path,name): Connecting directories with file names or directories
Os.path.basename (PATH): Return file name
Os.path.dirname (path): Back to File path
File operations in the OS module:
OS Module Properties
Linesep the string used to separate rows in a file
A string that the SEP uses to separate the file path names
Pathsep string used to separate file paths
CurDir the string name of the current working directory
Pardir (current working directory) parent directory string Name
1. Renaming: Os.rename (old, new)
2. Delete: Os.remove (file)
3. Listing files under the directory: Os.listdir (PATH)
4. Get the current working directory: OS.GETCWD ()
5. Change Working directory: Os.chdir (newdir)
6. Create a multilevel directory: Os.makedirs (r "C:\python\test")
7. Create a single directory: Os.mkdir ("Test")
8. Delete Multiple directories: Os.removedirs (r "C:\python") #删除所给路径最后一个目录下所有空目录.
9. Delete a single directory: Os.rmdir ("Test")
10. Get File properties: Os.stat (file)
11. Modify file permissions and timestamp: os.chmod (file)
12. Execute operating system command: Os.system ("dir")
13. Start a new process: Os.exec (), OS.EXECVP ()
14. In the background execution procedure: OSSPAWNV ()
15. Terminate the current process: Os.exit (), Os._exit ()
16. Separate file name: Os.path.split (r "c:\python\hello.py")--> ("C:\\python", "hello.py")
17. Detach Extension: Os.path.splitext (r "c:\python\hello.py")--> ("C:\\python\\hello", ". Py")
18. Get path name: Os.path.dirname (r "c:\python\hello.py")--> "C:\\python"
19. Get FileName: Os.path.basename (r "r:\python\hello.py")--> "hello.py"
20. Determine whether the file exists: os.path.exists (r "c:\python\hello.py")--> True
21. Determine whether it is an absolute path: Os.path.isabs (r ". \python\")--> False
22. Determine whether the directory: Os.path.isdir (r "C:\python")--> True
23. Judge whether it is a file: Os.path.isfile (r "c:\python\hello.py")--> True
24. Determine if the link file: Os.path.islink (r "c:\python\hello.py")--> False
25. Get File Size: os.path.getsize (filename)
26.*******:os.ismount ("c:\\")--> True
27. Search directory for all Files: Os.path.walk ()
Shutil module actions on files:
1. Copy a single file: Shultil.copy (Oldfile, Newfle)
2. Copy the entire tree: Shultil.copytree (r ". \setup", R ". \backup")
3. Delete entire directory tree: Shultil.rmtree (r. \backup)
Actions for temporary files:
1. Create a unique temporary file: tempfile.mktemp ()--> filename
2. Open temporary file: tempfile. Temporaryfile ()
Memory files (Stringio and Cstringio) operations
[4.StringIO] #cStringIO是StringIO模块的快速实现模块
1. Create the memory file and write the initial data: F = Stringio.stringio ("Hello world!")
2. Read into memory file data: Print F.read () #或print f.getvalue ()--> Hello world!
3. Write data to memory file: F.write ("Good day!")
4. Close Memory File: F.close ()
'''
Import OS
Import Os.path
Import UnitTest
Import time
#import Pygame
Class Pyfilecommonoperatortest (UnitTest. TestCase):
def __init__ (self):
"" "Constructor" ""
def test01 (self):
Print Os.linesep
Print Os.sep
Print Os.pathsep
Print Os.curdir
Print Os.pardir
Print OS.GETCWD ()
print ' UnitTest here '
if __name__ = = "__main__":
t = pyfilecommonoperatortest ()
T.TEST01 ()
Copy Code code as follows:
#读文件的写法:
#读文本文件:
input = open (' Data ', ' R ') #第二个参数是默认的, you can not add
#读二进制文件:
input = open (' Data ', ' RB ')
#读取所有文件内容:
Open (' Xxoo.txt '). Read ()
#读取固定字节
Open (' Abinfile ', ' RB '). Read (100)
#读每行
File_object.readlines ()