如何在驱动器中的每个文件夹上运行脚本



我正在网络驱动器中进行数据清理。驱动器有1000多个文件夹,这些文件夹有几个子文件夹。我从G4G得到的脚本(如下所示(提示我选择一个文件夹。我可以点击我的1000多个文件夹中的一个,数据就会被正确清理(重复的会被删除(。但是,我想在整个驱动器中循环使用该命令,以避免在数小时内单击文件夹。我无法选择驱动器作为我的文件夹,因为驱动器中第一个文件夹之间的重复文件名不应被视为重复。

示例:Z:/Folder1Z:/Folder2都有几个名为text.txt的文件,它们就在文件夹内部和文件夹的子目录中。Folder1Folder2,在其子目录内的所有text.txt文件中,应分别保留一个text.txt。如果当前脚本分别应用于Folder1Folder2,则实现了存在于Folder1Folder2中的一个text.txt文件的期望结果。如果脚本应用于Z:驱动器,那么在Folder1Folder2之间,将只有一个text.txt,并且其中一个文件夹将没有名为text.txt的文件。

如何将此脚本应用于驱动器中的每个第一个文件夹,而不必手动单击每个文件夹?

from tkinter.filedialog import askdirectory
# Importing required libraries.
from tkinter import Tk
import os
import hashlib
from pathlib import Path
# We don't want the GUI window of
# tkinter to be appearing on our screen
Tk().withdraw()
# Dialog box for selecting a folder.
file_path = askdirectory(title="Select a folder")
# Listing out all the files
# inside our root folder.
list_of_files = os.walk(file_path)
# In order to detect the duplicate
# files we are going to define an empty dictionary.
unique_files = dict()
for root, folders, files in list_of_files:
# Running a for loop on all the files
for file in files:
# Finding complete file path
file_path = Path(os.path.join(root, file))
# Converting all the content of
# our file into md5 hash.
Hash_file = hashlib.md5(open(file_path, 'rb').read()).hexdigest()
# If file hash has already #
# been added we'll simply delete that file
if Hash_file not in unique_files:
unique_files[Hash_file] = file_path
else:
if file.endswith((".txt",".bmp")):
os.remove(file_path)
print(f"{file_path} has been deleted")

您可以按如下方式更改脚本。基本上,下面的脚本获取当前目录中所有目录的绝对路径,并逐个提供给它们进行清理。

from tkinter.filedialog import askdirectory
# Importing required libraries.
from tkinter import Tk
import os
import hashlib
from pathlib import Path
# We don't want the GUI window of
# tkinter to be appearing on our screen
Tk().withdraw()
# Dialog box for selecting a folder.
file_paths = [os.path.abspath(i) for i in os.listdir() if os.path.isdir(i)]
for file_path in file_paths:
# Listing out all the files
# inside our root folder.
list_of_files = os.walk(file_path)
# In order to detect the duplicate
# files we are going to define an empty dictionary.
unique_files = dict()
for root, folders, files in list_of_files:
# Running a for loop on all the files
for file in files:
# Finding complete file path
file_path = Path(os.path.join(root, file))
# Converting all the content of
# our file into md5 hash.
Hash_file = hashlib.md5(open(file_path, 'rb').read()).hexdigest()
# If file hash has already #
# been added we'll simply delete that file
if Hash_file not in unique_files:
unique_files[Hash_file] = file_path
else:
if file.endswith((".txt",".bmp")):
os.remove(file_path)
print(f"{file_path} has been deleted")

相关内容

最新更新