File Operations in the python OS Module

Source: Internet
Author: User

File Operations in the OS module:
OS module attributes
Linesep is a string used to split rows in a file.
A string used to separate the file path name.
Pathsep is a string used to separate file paths.
String name of the current working directory of curdir
Pardir (current working directory) parent directory string name
1. Rename: OS. Rename (old, new)
2. Delete: OS. Remove (file)
3. List the files in the directory: OS. listdir (PATH)
4. Get 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. Execute the operating system command: OS. System ("dir ")
13. Start a new process: OS .exe C (), OS .exe CVP ()
14. Execute 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 name: OS. Path. dirname (R "C: \ Python \ hello. py") --> "C: \ Python"
19. Get the file name: 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 the path is absolute: OS. Path. isabs (R ". \ Python \") --> false
22. Check whether the directory is OS. Path. isdir (R "C: \ Python") --> true
23. Determine whether the file is: 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. Walk ()

Detailed source reference: http://www.jb51.net/article/28905.htm

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.