current position:Home>Save time and effort. 10 lines of Python code automatically clean up duplicate files in the computer
Save time and effort. 10 lines of Python code automatically clean up duplicate files in the computer
2022-01-30 05:29:43 【Yunyun yyds】
This article mainly introduces the use of Python Automatically clean up duplicate files in the computer , as long as 10 Line code , This article is very detailed , It has a certain reference value for everyone's study or work , Friends in need can refer to . Given a folder , Use Python Check for duplicate files in the given folder , Delete if there is a duplicate .
The main knowledge points involved are :
- os Integrated application of modules
- glob Integrated application of modules
- utilize filecmp Module compares two files
1. Step analysis
The logic of the program can be embodied as : Traverse to get all the files under the given folder , Then through nested loops, we compare whether the files are the same , If the same, delete the latter .
2. How to judge whether two files are the same ?
Here we can use filecmp modular , Take a look at the official introduction document :
- filecmp.cmp(f1, f2, shallow=True)
- The comparison name is f1 and f2 The file of , If they seem to be equal, return True, Otherwise return to False
- If shallow It's true , So have the same os.stat() Signed documents will be considered equal . otherwise , The contents of the file will be compared .
So it can be used in this way
# hypothesis x and y The two documents are the same
print(filecmp.cmp(x, y))
# True
Copy code
3.Python Implementation steps
1. Import the required library and set the destination folder path
import os
import glob
import filecmp
dir_path = r'C:\\xxxx'
Copy code
2. Then traverse to get the absolute path of all files , We can use glob The combination of wildcards of modules recursive Parameters can be used to complete , The framework is as follows :
for file in glob.glob(path + '/**/*', recursive=True):
pass
Copy code
3. After traversing to get each file or folder , Need to determine if it's a file , If it is a file, the absolute path may be stored in the list , Two more things need to be done here :
- First create an empty list , Later use list.append(i) Add file path
- Then use os.path.isfile(i) Determine if it's a document , return True The operation of adding elements is performed
The code is as follows :
file_lst = []
for i in glob.glob(dir_path + '/**/*', recursive=True):
if os.path.isfile(i):
file_lst.append(i)
Copy code
4. In the last step, we get all the file paths in the target folder , Next, you can nest and traverse the path list , among filecmp.cmp
File judgment ,os.remove
Delete the file
for x in file_lst:
for y in file_lst:
if x != y:
if filecmp.cmp(x, y):
os.remove(y)
Copy code
5. The code here has implemented the general logic , But there is one detail to consider : It is possible to loop to the file has been deleted by the previous judgment , Lead to os.remove(file)
Error reported because the file does not exist , therefore , It can be used os.path.exists
Judge the existence of the file , As shown below :
for x in file_lst:
for y in file_lst:
if x != y and os.path.exists(x) and os.path.exists(y):
if filecmp.cmp(x, y):
os.remove(y)
Copy code
4. Complete code
such , A simple file to duplicate the small program completed .
import os
import glob
import filecmp
dir_path = r'C:\xxxx'
file_lst = []
for i in glob.glob(dir_path + '/**/*', recursive=True):
if os.path.isfile(i):
file_lst.append(i)
for x in file_lst:
for y in file_lst:
if x != y and os.path.exists(x) and os.path.exists(y):
if filecmp.cmp(x, y):
os.remove(y)
Copy code
Through the Python Automate the scripting process , Can feel Python The power of office automation , There are more ways to use office automation , You can pay attention to me , Share more dry goods later .
Here comes the welfare , Click to collect ①Python300 This related e-book , ② Technical communication , ③ Tool installation package , ④ Order receiving AC , ⑤ Interesting source sharing .
copyright notice
author[Yunyun yyds],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300529414710.html
The sidebar is recommended
- Python collects and monitors system data -- psutil
- Python chat room (Tkinter writing interface, streaming, socket to realize private chat, group chat, check chat records, Mysql to store data)
- Learning this Python library can reduce at least 100 lines of code
- leetcode 67. Add Binary(python)
- Regular re parameter replacement of Python 3 interface automation test framework
- V. pandas based on Python
- Only 15 lines of code is needed for face detection! (using Python and openCV)
- [Python crawler Sao operation] you can crawl Sirius cinema movies without paying
- leetcode 69. Sqrt(x)(python)
- Teach you to read the source code of Cpython (I)
guess what you like
-
Snowball learning started in the fourth quarter of Python. One needs three meals. I have a new understanding of Python functional programming, process-oriented, object-oriented and functional
-
leetcode 88. Merge Sorted Array(python)
-
Don't you know more about a python library before the end of 2021?
-
Python crawler web page parsing artifact XPath quick start teaching!!!
-
Use Python and OpenCV to watermark the image
-
String and related methods of Python data type introduction
-
Heapq module of Python module
-
Introduction to beautiful soup of Python crawler weapon, detailed explanation, actual combat summary!!!
-
Event loop of Python collaboration series
-
Django docking pin login system
Random recommended
- [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos
- You used to know Python advanced
- Pyinstaller package Python project
- 2021 IEEE programming language rankings: Python tops the list!
- Implementation of Python automatic test control
- Python advanced: [Baidu translation reverse] graphic and video teaching!!!
- Do you know the fuzzy semantics in Python syntax?
- [Python from introduction to mastery] (XXVII) learn more about pilot!
- Playing excel office automation with Python
- Some applications of heapq module of Python module
- Python and go languages are so popular, which is more suitable for you?
- Python practical skills task segmentation
- Python simulated Login, numpy module, python simulated epidemic spread
- Python opencv contour discovery function based on image edge extraction
- Application of Hoff circle detection in Python opencv
- Python reptile test ox knife (I)
- Day 1: learn the Django framework of Python development
- django -- minio_ S3 file storage service
- [algorithm learning] 02.03 Delete intermediate nodes (Java / C / C + + / Python / go)
- Similarities and differences of five pandas combinatorial functions
- Learning in Python + opencv -- extracting corners
- Python beginner's eighth day ()
- Necessary knowledge of Python: take you to learn regular expressions from zero
- Get your girlfriend's chat records with Python and solve the paranoia with one move
- My new book "Python 3 web crawler development practice (Second Edition)" has been recommended by the father of Python!
- From zero to familiarity, it will take you to master the use of Python len() function
- Python type hint type annotation guide
- leetcode 108. Convert Sorted Array to Binary Search Tree(python)
- For the geometric transformation of Python OpenCV image, let's first talk about the extraordinary resize function
- leetcode 701. Insert into a Binary Search Tree (python)
- For another 3 days, I sorted out 80 Python datetime examples, which must be collected!
- Python crawler actual combat | using multithreading to crawl lol HD Wallpaper
- Complete a python game in 28 minutes, "customer service play over the president card"
- The universal Python praise machine (commonly known as the brushing machine) in the whole network. Do you want to know the principle? After reading this article, you can write one yourself
- How does Python compare file differences between two paths
- Common OS operations for Python
- [Python data structure series] linear table - explanation of knowledge points + code implementation
- How Python parses web pages using BS4
- How do Python Network requests pass parameters
- Python core programming - decorator