Fundamentals 4 min read

Using Python's os Module to Delete Duplicate Files

This article demonstrates a simple Python method for identifying and removing duplicate files by leveraging the built‑in os module, explaining how to list directory contents, construct file paths, and use os.remove() to delete unwanted copies, complete with example code and usage notes.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Using Python's os Module to Delete Duplicate Files

In everyday computing, duplicate files often accumulate, making manual cleanup tedious, especially when many duplicates exist.

This guide introduces a convenient method using Python to delete duplicate files.

Python's built‑in os module provides functions for file system operations; by listing directory contents and removing unwanted files with os.remove(path) , duplicates can be eliminated.

The example code below demonstrates loading the os library, listing files in a specified directory, selecting files to delete, constructing full file paths with os.path.join , and calling os.remove to delete them, followed by printing the remaining files.

<code>import os  # load file management library
path = "D:\\projects"
files = os.listdir(path)    # list all file names in the path as a list
print(type(files))          # verify the type of files
print("路径:{} 下拥有的文件是{}".format(path, files))  # print all file names
files_delete = files[0:2]   # select files to delete (can also use input)
print(files_delete)         # print selected file names
for file_name in files_delete:
    file_path = os.path.join(path, file_name)  # join path and file name
    os.remove(file_path)                       # delete the file
print("删除重复文件之后, path下的文件名有哪些:", os.listdir(path))  # list remaining files</code>

After running the script, the duplicate files are removed from the specified directory.

Additional notes explain that os.listdir(path) returns a list of file names, os.path.join concatenates directory and file name into a full path, and os.remove(path) deletes the file at the given path.

PythonAutomationfile managementos moduleDuplicate Files
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.