Python compares multiple files in a folder with the same name and copies them.

Source: Internet
Author: User
This article mainly introduces how to compare and copy more files in a folder than in another folder with the same name. it involves Python's file and folder operation skills and has some reference value, for more information, see the example in this article. Share it with you for your reference. The details are as follows:

This stuff was originally used for data synchronization for the company: the new server has not been officially activated, the old server is still in use, and a large number of images are uploaded to the old server every day, this tool is written to avoid backing up all images.

1. shows the running effect:

2. the Python code is as follows:

The code is as follows:

# Coding = gbk
'''
Created on 2011-1-7
@ Author: HH
'''
Import OS, ConfigParser
'''
Recursively List objects in a directory and put 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 content in the List to a file
'''
Def writeListToFile (list, path ):
Strs = "\ n". join (list)
F = open (path, 'WB ')
F. write (strs)
F. close ()
'''
Read file content and put it in List
'''
Def readFileToList (path ):
Lists = []
F = open (path, 'RB ')
Lines = f. readlines ()
For line in lines:
Lists. append (line. strip ())
F. close ()
Return lists
'''
Compare files -- Set
'''
Def compList (list1, list2 ):
Return list (set (list1)-set (list2 ))
'''
Copy the files in the List to the 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 file:" + file
Open (targetFile, 'WB '). write (open (file, 'RB'). read ())
Else:
Print "the file already exists. do not copy it! "
If _ name _ = '_ main __':
Path = ". svn"
# Getting the source directory
TxtFile = "1.txt"
# Target file output by directory structure
Tdir = "cpfile"
# Target directory to be copied
CfFile = "config. ini ";
# Configuration file name
FileList = []
# Reading configuration files
If (OS. path. exists (cfFile )):
Cf = ConfigParser. ConfigParser ()
Cf. read (cfFile)
Path = cf. get ("main", "sourceDir ")
TxtFile = cf. get ("main", "txtFile ")
Tdir = cf. get ("main", "targetDir ")
Else:
Print "the configuration file does not exist! "
Raw_input ("\ n press enter to exit \ n ")
Exit ()
If (OS. path. exists (txtFile )):
# If the exported file exists, read and compare it
List1 = readFileToList (txtFile)
Print "reading the file list ...... "
FileList = listDir (fileList, path)
Print "comparing files ...... "
List_res = compList (fileList, list1)
If len (list_res)> 0:
Print "the following files do not exist in the original Directory: \ n"
Print "\ n". join (list_res)
Print "\ n total files:" + str (len (list_res) + "\ n"
If raw_input ("\ n: is the file copied? (Y/n )")! = 'N ':
CopyFiles (list_res, tdir)
Else:
Print "no different files! "
Else:
# If the exported file does not exist, the exported file
Print "reading the 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:

The code is as follows:

# Configuration File name: config. ini
[Main]
SourceDir = wwwroot
Txtfile00001.txt
TargetDir = cp

I hope this article will help you with Python programming.

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.