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