gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:37:45 26 4
gpt4 key购买 nike

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

例子:Z:/Folder1Z:/Folder2 都有几个名为 text.txt 的文件,紧挨着文件夹和子目录文件夹。 Folder1Folder2,在其子目录内的所有 text.txt 文件中,每个文件都应保留一个 text.txt 。如果当前脚本分别应用于 Folder1Folder2,则 Folder1< 中存在的一个 text.txt 文件的期望结果Folder2 中的一个已完成。如果脚本应用于 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")

关于python - 如何在驱动器中的每个文件夹上运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73066745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com