Use Python to find the same files in two folders and save them in a new folder (in three cases)

Source: Internet
Author: User

Original Folder A, B, new Folder C, in the case of pictures as an example

A:00001.jpg 00002.jpg 00003.jpg 00147.jpg

B:00001.jpg 000000002.jpg 00147.json

The first case: Find files with the same contents under two folders, save and output to folder C

Idea: Determine whether the content is consistent, so you need to read the entire file to determine whether the two are the same

Because the content of the file is complex and its MD5 is unique, the MD5 value should be the same if both contents are consistent. Since the picture is binary storage, it uses ' RB ' when reading. Here is the MD5 value processing for the file contents. The type of each file name is str format. Generate a dictionary of files under a file to determine if the file under B is in a generated dictionary, and if it exists, it will exist under the new folder C.

The code is:

The second case: if the file name in the A and B folders is consistent, do not care about the consistency of the content, if the file name is consistent, save in the new folder. The difference between this and the situation one is that the file name is MD5 processed.

The filename needs to be encoded and will be error-coded.

The third case: the file name in B and the files in the a folder, such as a file under the Jian folder 000147.json, need to be based on the method of string extraction to determine whether there is content consistent

According to the actual situation to make corresponding adjustments.

Reference article link: https://zhidao.baidu.com/question/984682642027858179.html

Use Python to find the same files in two folders and save them in a new folder (in three cases)

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.