Python writes some operations for automated files and directories (new, moved, deleted)

Source: Internet
Author: User

To create a file:

OS.Mknod("Test.txt")   #创建孔文件Open("Test.txt",W)   #直接打开一个文件, create a file if the file does not exist
To Create a directory:

OS . mkdir ("file")
Copying Files

Shutil.CopyFile("Oldfile","NewFile")   #oldfile and NewFile can only be files .Shutil.Copy("Oldfile","NewFile")   #oldfile can only be a folder, NewFile may make a file, or it can be a directory
Copy Folder

Shutil . Copytree ("Olddir","Newdir") both #oldfile and NewFile can only be directories, and newdir must not exist   
To rename a file or directory:

OS . Rename ("Oldname","NewName")
Moving Files (directories):

Shutil . Move ("Oldpos","Newpos")
To delete a file:

OS . Remove ("file")
To Delete a directory:

OS.rmdir("dir")   #只能删除空目录Shutil.Rmtree("dir")   #空目录, content directories can be deleted
To convert a directory:

OS . chdir ("path")
determine whether a directory or a file:

OS.Path.exists("Goal")   #判断目标是否存在OS.Path.Isdir("Goal")   #判断目标是否是目录OS.Path.Isfile("Goal")   #判断目标是否是文件

Python writes some operations for automated files and directories (new, moved, deleted)

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.