The following is a copy of all files that end with. jpg in the SourceDir directory to the TARGETDIR directory:
<span style= "FONT-SIZE:18PX;" >>>>import os>>> Import os.path>>> import shutil >>> def copyfiles (SourceDir, TargetDir): For files in Os.listdir (sourcedir): SourceFile = Os.path.join (sourcedir,files) // Link the folder name and file name targetfile = Os.path.join (targetdir,files) if Os.path.isfile (sourcefile) and Sourcefile.find ('. JPG ') >0://requirement is file and suffix is jpg
Shutil Module
Copy Folder
Copying files
When you copy a file. Assume that there is a folder between the specified file destination locations that does not exist. The error will be thrown.
So it's best to confirm the folder exists between copies.
When the folder exists, there is no problem copying the files.
Delete a folder using such functions as the following:
Shutil.rmtree (' D:/dd ')
Move a file or directory to another place:
Shutil.move (' d:/c.png ', ' e:/')
-------------------------------------------
Then there is a problem. What is the difference between the copy function and the CopyFile function?
See Help:
From help can be seen, copyfile but copy files to the destination file. But the copy function can also copy the file's mode. For example, the original file has +x permission to run, then the destination file will have the permissions to run.
Delete all files under the first-level folder:
<span style= "FONT-SIZE:18PX;" >def Removefileinfirstdir (TargetDir): For file in Os.listdir (targetDir): targetfile = Os.path.join ( TargetDir, file ) if Os.path.isfile (targetfile)://delete files only do not delete directories os.remove (targetfile) </span>
Copy the text content and write the contents of all the files in the folder to the target file:
<span style= "FONT-SIZE:18PX;" >def coverfiles (SourceDir, targetDir): For file in Os.listdir (sourcedir): sourcefile = Os.path.join ( SourceDir, file) targetfile = Os.path.join (targetDir, file) #cover the files//replication? if Os.path.isfile (sourcefile): open (TargetFile, "WB"). Write (Open (sourcefile, "RB"). Read ()) </span>
<span style= "FONT-SIZE:18PX;" >def Writeversioninfo (targetDir): open (TargetDir, "WB"). Write ("Revison:") </span>
It is very convenient to use a Python script for file operation. Saves a lot of time
Python file operations-copy, cut, delete, etc.