Python compares a folder to a file that is more than another folder with the same name and copies the method _python

Source: Internet
Author: User

This example describes a method that the Python comparison folder has more files than another folder with the same name and is copied. Share to everyone for your reference. Specifically as follows:

This dongdong is to do to the company data synchronization: The new server has not been officially enabled, the old server is still in use, a large number of pictures uploaded to the old server every day, in order to avoid back up all the pictures, so write a tool.

1. The operation effect is shown in the following illustration:

2. The Python code is as follows:

Copy Code code as follows:
Created on 2011-1-7
@author: HH
Import Os,configparser
Recursively lists the files in a directory and puts them in the list.
def listdir (Filelist,path):
Files=os.listdir (PATH)
For I in Files:
file_path=path+ "\" +i
If Os.path.isfile (File_path):
Filelist.append (File_path)
For I in Files:
file_path=path+ "\" +i
If Os.path.isdir (File_path):
#fileList. Append (File_path)
Listdir (Filelist,file_path)
return filelist
Write the contents of a list to a file
def writelisttofile (List,path):
Strs= "\ n". Join (list)
F=open (Path, ' WB ')
F.write (STRs)
F.close ()
Read the contents of the file and put it in the list
def readfiletolist (path):
F=open (Path, ' RB ')
Lines=f.readlines ()
For line in lines:
Lists.append (Line.strip ())
F.close ()
return lists
Compare files--in Set mode
def complist (LIST1,LIST2):
Return list (set (List1)-set (LIST2))
Copy files from list to specified location
def CopyFiles (Filelist,targetdir):
For file in FileList:
Targetpath=os.path.join (targetdir,os.path.dirname (file))
Targetfile=os.path.join (Targetdir,file)
If not os.path.exists (TargetPath):
Os.makedirs (TargetPath)
If not os.path.exists (targetfile) or (Os.path.exists (targetfile) and Os.path.getsize (targetfile)!=os.path.getsize ( file)):
print "Copying files:" +file
Open (TargetFile, ' WB '). Write (open (file, ' RB '). Read ())
Print file already exists and does not replicate! "
if __name__ = = ' __main__ ':
Path= ". SVN"
Txtfile= "1.txt"
Tdir= "Cpfile"
Cffile= "Config.ini";
if (os.path.exists (cffile)):
Cf=configparser.configparser () (Cffile)
Path=cf.get ("main", "SourceDir")
Txtfile=cf.get ("main", "txtfile")
Tdir=cf.get ("main", "TargetDir")
The print configuration file does not exist! "
Raw_input ("\ n press Enter to exit \ n")
Exit ()
if (os.path.exists (txtfile)):
#如果导出的文件存在, compare after reading
List1=readfiletolist (txtfile)
Print "Reading file list ..."
Filelist=listdir (Filelist,path)
Print "Is comparing files ..."
List_res=complist (Filelist,list1)
If Len (list_res) >0:
Print "Below is a file that does not exist in the original directory: \ n"
print "\ n". Join (List_res)
print "\ n Total Files:" +str (Len (list_res)) + "\ n"
If Raw_input ("\ nthe files are copied?") (y/n) ")!= ' n ':
CopyFiles (List_res,tdir)
Print "does not have the same file!" "
#如果导出的文件不存在, the file is exported
Print "Reading file list ..."
Filelist=listdir (Filelist,path)
Writelisttofile (Filelist,txtfile)
Print "Saved to file:" +txtfile
Raw_input ("\ n press Enter to exit \ n")

3. Configuration file name: Config.ini as follows:
Copy Code code as follows:
#配置文件名: Config.ini

I hope this article will help you with your Python programming.

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: 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.