Python common file operations function example

Source: Internet
Author: User
#-*-Coding: utf8-*-''' common Python File Operation example OS. in the path module, access the function to separate the basename () directory path, return the file name dirname (), remove the file name, return the directory path join (), and combine the separated parts into a path name split () return (dirname (), basename () tuples splitdrive () returns (drivename, pathname) tuples splitext () returns (filename, extension) tuples information getatime () returns the latest access time getctime () returns the File Creation Time getmtime () returns the latest file modification time getsize () returns the file size (in bytes) query exists () whether isabs () exists in the specified path (file or directory) and whether the specified path is isdir (). Whether the path exists and specifies whether the path exists for a directory isfile () and whether the path exists for a file islink () and ismount () specifies whether the path exists and is a mount point samefile (). Whether the two path names point to the same file OS. path. isdir (name): determines whether the name is a directory. If the name is not a directory, false OS is returned. path. isfile (name): checks whether the name is a file. If the name does not exist, false OS is returned. path. exists (name): determines whether a file or directory name OS exists. path. getsize (name): Get the file size. If the name is a directory, return 0l OS. path. abspath (name): Obtain the absolute path OS. path. normpath (PATH): OS. path. split (name): Split the file name and directory (in fact, if you fully use the object It also separates the last directory as the file name, and does not determine whether the file or directory exists) OS. path. splitext (): separates the file name and extension OS. path. join (path, name): Connection directory and file name or directory OS. path. basename (PATH): returns the file name OS. path. dirname (PATH): returns the file operation in the file path OS module: OS module attribute linesep is used to split the line string SEP in the file to separate the path string pathsep is used to separate the file path string curdir current working directory string name pardir (current working directory) parent directory string name 1. rename: OS. rename (old, new) 2. delete: OS. remove (File) 3. list Files in the directory: OS. listdir (PATH) 4. obtain the current working directory: OS. getcwd () 5. change the working directory: OS. ch Dir (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") # delete all empty directories in the last directory of the given path. 9. delete a single directory: OS. rmdir ("test") 10. get file attributes: OS. stat (File) 11. modify the File Permission and timestamp: OS. chmod (File) 12. run the operating system command: OS. system ("dir") 13. start a new process: OS .exe C (), OS .exe CVP () 14. run the program in the background: osspawnv () 15. terminate the current process: OS. exit (), OS. _ exit () 16. separated file name: OS. path. split (R "C: \ Python \ hello. PY ") --> (" C: \ Python "," Hello. PY ") 17. separation Extension: OS. path. splitext (R "C: \ Python \ hello. PY ") --> (" C: \ Python \ hello ",". PY ") 18. obtain the path: 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 whether a file exists: OS. path. exists (R "C: \ Python \ hello. PY ") --> true 21. determine whether the path is absolute: OS. path. isabs (R ". \ Python \ ") --> false 22. determine whether the directory is OS. path. isdir (R "C: \ Python") --> true 23. determine whether it is a file: OS. path. isfile (R "C: \ Python \ hello. PY ") --> true 24. determine whether the file 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 the directory: OS. path. operations on files by the walk () shutil module: 1. copy a single file: shultil. copy (oldfile, newfle) 2. copy the entire directory tree: shultil. copytree (R ". \ setup ", R ". \ backup ") 3. delete the entire directory tree: shultil. rmtree (R ". \ backup ") Temporary File Operations: 1. create a unique temporary file: tempfile. mktemp () --> filename 2. open temporary file: tempfile. temporaryfile () memory file (stringio and cstringio) operations [4. stringio] # cstringio is the fast implementation module of the stringio module. create a memory file and write the initial data: F = stringio. string Io ("Hello world! ") 2. Read the data in the memory file: Print F. Read () # or print F. getvalue () --> Hello world! 3. Write Data to memory files: f. Write ("good day! ") 4. disable memory files: f. close () ''' import osimport OS. pathimport unittestimport time # import pygameclass 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 where' if _ name _ = "_ main _": t = pyfilecommonoperatortest () T. test01 ()

# Writing a Read File: # reading a text file: input = open ('data', 'R') # The second parameter is default. You can leave it blank. # Read a binary file: input = open ('data', 'rb') # Read all file content: open('xxoo.txt '). read () # read the fixed byte open ('abinfile', 'rb '). read (100) # read each row of file_object.readlines ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.