Py file operations

Source: Internet
Author: User

1. OS]

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 ()

[2. shutil]

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 ")

[3. tempfile]

1. Create a unique temporary file: tempfile. mktemp () --> filename

2. Open the temporary file: tempfile. temporaryfile ()

[4. stringio] # cstringio is a fast implementation module of the stringio module.

1. Create a memory file and write the initial data: F = stringio. stringio ("Hello world! ")

2. Read Memory file data: Print F. Read () # or print F. getvalue () --> Hello world!

3. Write Data to memory files: f. Write ("good day! ")

4. Close the memory file: f. Close ()

[5. glob]

1. Matching file: Glob. glob (R "C:/Python/*. py ")

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.