我想做一些东西来检查我的文件夹并找到重复的。也就是说,文件不能有相同的名称,所以我做的第一部分是遍历文件夹并附加一个文件夹列表。然后我想把列表中的项目分解成列表,并相互比较,找到高相似性。我被第二部分困住了,不知道如何接近。如果有人能提供一些启示,那就太好了,谢谢!
import os
path = input("Where you want to look?")
myFolder = list()
print("Here's your folders:")
for dirname in os.listdir(path):
f = os.path.join(path,dirname)
if os.path.isdir(f):
myFolder.append(f)
print("n".join(myFolder))
print(len(myFolder), "folders found!")
我正在考虑创建一个列表的字典,每个列表是一个文件夹名称按字母分解
首先,我建议您使用pathlib
库,因为os
对于文件浏览来说有点过时了。你可以这样做:
from pathlib import Path
folder_path = Path(input("Where you want to look?"))
folder_content = [file_or_dir for file_or_dir in folder_path.iterdir() if file_or_dir.is_file()]
# folder_content's code is the equivalent of:
folder_content = []
for file_or_dir in folder_path.iterdir(): # listing the files of folder_path
if file_or_dir.is_file(): # checking if file_or_dir is a file or not
folder_content.append(file_or_dir) # if it is, we add it to the list folder_content
# Then we check for duplicates
names_list = [file.name for file in folder_content] # We make a list that contains all of the names
for name in names_list:
if names_list.count(name) > 1: # if find more than one same name in the names_list
print(f'Name "{name}" found more than one time !!!')