Common functions of the Python OS file operation module

Source: Internet
Author: User

We often deal with files and directories. For these operations, python provides an OS module that contains a lot of functions for operating files and directories. All functions can be viewed using help (OS) or dir (OS.

Common functions are as follows:

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. 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") # 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 or directory exists: OS. path. exists (r "c: \ python \ hello. py ") --> True21. determine whether the path is absolute: OS. path. isabs (r ". \ python \ ") --> False22. determine whether it is a directory: OS. path. isdir (r "c: \ python") --> True23. determine whether it is a file: OS. path. isfile (r "c: \ python \ hello. py ") --> True24. determine whether it is a linked file: OS. path. islink (r "c: \ python \ hello. py ") --> False25. get the file size: OS. path. getsize (filename) 26. search all files in the directory: OS. path. walk ()

This article from the "Wang Wei" blog, please be sure to keep this source http://wangwei007.blog.51cto.com/68019/1217082

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.