[Python]View PlainCopyprint?
- #-*-coding:utf8-*-
- "' "
- Python common file Operations examples
- Path name access functions in the Os.path module
- Separated
- basename () Remove directory path, return file name
- DirName () Remove file name, return directory path
- Join () combines parts of the separation into one path name
- Split () return (DirName (), basename ()) tuple
- Splitdrive () return (drivename, pathname) tuple
- Splitext () return (filename, extension) tuple
- Information
- Getatime () returns the last access time
- Getctime () returns file creation time
- Getmtime () returns the last 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): Determine if Name is a directory, name is not a directory and return false
- Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false
- Os.path.exists (name): Determine if there is a file or directory name
- 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 file name, and it will not determine whether the file or directory exists)
- Os.path.splitext (): Detach file name and extension
- Os.path.join (path,name): Connection directory with file name or directory
- Os.path.basename (PATH): Return file name
- Os.path.dirname (PATH): Return file path
- File operations in the OS module:
- OS Module Properties
- Linesep string used to separate rows in a file
- The string that Sep uses to separate the file path name
- 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. List files in directory: Os.listdir (PATH)
- 4. Get current working directory: OS.GETCWD ()
- 5. Change of 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 attributes: Os.stat (file)
- 11. Modify file permissions and timestamps: Os.chmod (file)
- 12. Execute operating system command: Os.system ("dir")
- 13. Start a new process: Os.exec (), OS.EXECVP ()
- 14. Executing the program in the background: OSSPAWNV ()
- 15. Terminate the current process: Os.exit (), Os._exit ()
- 16. Detach 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 File Name: Os.path.basename (r "r:\python\hello.py")--"hello.py"
- 20. Determine if the file exists: os.path.exists (r "c:\python\hello.py")---True
- 21. Determine if absolute path: Os.path.isabs (r ". \python\")---False
- 22. Determine if it is a directory: Os.path.isdir (r "C:\python")--True
- 23. Determine if it is a file: Os.path.isfile (r "c:\python\hello.py")---True
- 24. Determine if it is a linked 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 all files in directory: Os.path.walk ()
- Shutil the operation of the module to the file:
- 1. Copying individual files: shultil.copy (Oldfile, Newfle)
- 2. Copy Entire directory tree: Shultil.copytree (r ". \setup", R ". \backup")
- 3. Delete entire directory tree: Shultil.rmtree (r ". \backup")
- Operations 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 in memory file data: Print F.read () #或print f.getvalue ()--Hello world!
- 3. Want the memory file to write data: F.write ("Good day!")
- 4. Close the 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 ()
[Python]View PlainCopyprint?
[Python]View PlainCopyprint?
- #读文件的写法:
- #读文本文件:
- input = open (' data ', ' R ')#第二个参数是默认的, can not be added
- #读二进制文件:
- input = open (' data ', ' RB ')
- #读取所有文件内容:
- Open (' xxoo.txt '). Read ()
- #读取固定字节
- Open (' abinfile ', ' RB '). Read (+)
- #读每行
- File_object.readlines ()
Common operations for Python files