Python's Directory file operation

Source: Internet
Author: User

[1.os]

1. Renaming: Os.rename (old, new)

2. Delete: Os.remove (file)

3. List the directories underFile: Os.listdir (PATH)

4. Get current working directory: OS.GETCWD ()

5. Change of 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") #删除所给路径最后一个目录下所有空目录.

9. Delete a single directory: Os.rmdir ("Test")

10. Get File attributes: Os.stat (file)

11. Modify file permissions and timestamps: Os.chmod (file)

12. Perform the actionSystem command: Os.system ("dir")

13. Start a new process: Os.exec (), OS.EXECVP ()

14. Executing the program in the background: OSSPAWNV ()

15. Terminate the current process: Os.exit (), Os._exit ()

16. Detach file Name: Os.path.split (r "c:/python/hello.py")--("C://python", "hello.py")

17. Detach Extension: Os.path.splitext (r "c:/python/hello.py")--("C://python//hello", ". Py")

18. Get path name: 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 if the file exists: os.path.exists (r "c:/python/hello.py")---True

21. Determine if absolute path: Os.path.isabs (r "./python/")---False

22. Determine if it is a directory: Os.path.isdir (r "C:/python")--True

23. Determine if it is a file: Os.path.isfile (r "c:/python/hello.py")---True

24. Determine if it 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 directory: Os.path.walk ()

[2.shutil]

1. Copying individual files: shultil.copy (Oldfile, Newfle)

2. Copy Entire directory tree: Shultil.copytree (r "./setup", R "./backup")

3. Delete entire directory tree: Shultil.rmtree (r "./backup")

[3.tempfile]

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

2. Open temporary file: tempfile. Temporaryfile ()

[4.StringIO] #cStringIO是StringIO模块的快速实现模块

1. Creatememory file and write initial data: F = Stringio.stringio ("Hello world!")

2. Read in memory file data: Print F.read () #或print f.getvalue ()--Hello world!

3. Want the memory file to write data: F.write ("Good day!")

4. Close the memory file: F.close ()

Python's Directory file operation

Related Article

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.