有没有办法根据两者中的匹配字符串将文件放入文件夹中?
Is there a way to place files into a folder based on matching strings in both?
我是 python 的新手,我将此作为我的第二次体验来继续我的学习。我的第一个项目(使用 pandas 进行数据分析)会比这个更难,但这将是一个新领域,我需要一些帮助才能开始,因为我什至不知道在任何文档中搜索什么.
我在一个目录中有很多以电视节目命名的文件夹。我在另一个目录中有许多文件,其中有多个节目的多个剧集。一个问题是,当我下载每一集时,它都存储在同名文件夹中。到目前为止,我一直在手动组织文件,但它已经到了自动化的地步(也是一个很好的学习练习)。有没有办法在 'Downloads' 文件夹中搜索包含包含字符串的文件的文件夹,比如 'Homeland',并将该文件(剧集)移动到标题为 "Homeland" 的文件夹中其他目录?我还需要为每个 file/folder 匹配多个字符串,例如 "Game" 和 "Thrones"。将它们移动到目录很容易,但是获取匹配的字符串是我希望获得一些见解的地方。那么我的下一个目标是遍历 'Downloads' 中的每个文件并将其分类到正确的电视节目文件夹中。
folders = 'list of folders in downloads'
#maybe I need to create a list here or a function that creates a list?
source_dir = "C:\Users\Downloads"
destination_dir = "C:\Users\TV Shows"
for folder_names in folders:
if folder_name contains destination_name:
# destination_name will be undefined but this is what i want
source_path = str(source_dir) + str(file_name) + str(.mp4)
destination_path = str(destination_dir) + str(file_name) +
str(.mp4)
shutil.move(source_path, destination_path)
if not:
do nothing
必须对其进行更改,因为有些变量会产生错误并且语法错误,但这是我想要的总体思路。
如果您有很多文件和文件夹,则使用 for
循环来处理它们。
您必须将文件名拆分为单词 - split(' ')
- 并使用 for
-loop 分别检查文件夹名称中的每个单词,并对文件夹名称中的单词进行计数。当计数为 2 或更多时,则移动文件。
或多或少:
all_filenames = [
'Game of Throne part II.mp4',
'other file.mp4',
]
all_folders = [
'Game Throne',
'Other Files'
]
for filename in all_filenames:
words = filename.lower().split(' ')
moved = False
for folder in all_folders:
count = 0
for word in words:
if word in folder.lower():
count += 1
if count >= 2:
print('count:', count, '|', filename, '->', folder)
# TODO: move file
moved = True
break
if not moved:
print('not found folder for:', filename)
# TODO: you could move file to `Other Files`
编辑: 获取所有匹配文件夹并要求用户选择正确文件夹的版本。
我没有测试。它可能需要更多代码来检查用户 select 号码是否正确。并最终添加选项以跳过它而不移动文件。
all_filenames = [
'Game of Throne part II.mp4',
'other file.mp4',
]
all_folders = [
'Game Throne',
'Other Files'
]
for filename in all_filenames:
words = filename.lower().split(' ')
matching = []
for folder in all_folders:
count = 0
for word in words:
if word in folder.lower():
count += 1
if count >= 2:
print('count:', count, '|', filename, '->', folder)
matching.append(folder)
#if not matching:
if len(matching) == 0:
print('not found folder for:', filename)
# TODO: you could move file to `Other Files`
elif len(matching) == 1:
print('move:', filename, '->', matching[0])
# TODO: move file to folder matching[0]
else:
for number, item in enumerate(matching):
print(number, item)
answer = int(input('choose number:'))
print('move:', filename, '->', matching[answer])
# TODO: move file to folder matching[answer]
我是 python 的新手,我将此作为我的第二次体验来继续我的学习。我的第一个项目(使用 pandas 进行数据分析)会比这个更难,但这将是一个新领域,我需要一些帮助才能开始,因为我什至不知道在任何文档中搜索什么.
我在一个目录中有很多以电视节目命名的文件夹。我在另一个目录中有许多文件,其中有多个节目的多个剧集。一个问题是,当我下载每一集时,它都存储在同名文件夹中。到目前为止,我一直在手动组织文件,但它已经到了自动化的地步(也是一个很好的学习练习)。有没有办法在 'Downloads' 文件夹中搜索包含包含字符串的文件的文件夹,比如 'Homeland',并将该文件(剧集)移动到标题为 "Homeland" 的文件夹中其他目录?我还需要为每个 file/folder 匹配多个字符串,例如 "Game" 和 "Thrones"。将它们移动到目录很容易,但是获取匹配的字符串是我希望获得一些见解的地方。那么我的下一个目标是遍历 'Downloads' 中的每个文件并将其分类到正确的电视节目文件夹中。
folders = 'list of folders in downloads'
#maybe I need to create a list here or a function that creates a list?
source_dir = "C:\Users\Downloads"
destination_dir = "C:\Users\TV Shows"
for folder_names in folders:
if folder_name contains destination_name:
# destination_name will be undefined but this is what i want
source_path = str(source_dir) + str(file_name) + str(.mp4)
destination_path = str(destination_dir) + str(file_name) +
str(.mp4)
shutil.move(source_path, destination_path)
if not:
do nothing
必须对其进行更改,因为有些变量会产生错误并且语法错误,但这是我想要的总体思路。
如果您有很多文件和文件夹,则使用 for
循环来处理它们。
您必须将文件名拆分为单词 - split(' ')
- 并使用 for
-loop 分别检查文件夹名称中的每个单词,并对文件夹名称中的单词进行计数。当计数为 2 或更多时,则移动文件。
或多或少:
all_filenames = [
'Game of Throne part II.mp4',
'other file.mp4',
]
all_folders = [
'Game Throne',
'Other Files'
]
for filename in all_filenames:
words = filename.lower().split(' ')
moved = False
for folder in all_folders:
count = 0
for word in words:
if word in folder.lower():
count += 1
if count >= 2:
print('count:', count, '|', filename, '->', folder)
# TODO: move file
moved = True
break
if not moved:
print('not found folder for:', filename)
# TODO: you could move file to `Other Files`
编辑: 获取所有匹配文件夹并要求用户选择正确文件夹的版本。
我没有测试。它可能需要更多代码来检查用户 select 号码是否正确。并最终添加选项以跳过它而不移动文件。
all_filenames = [
'Game of Throne part II.mp4',
'other file.mp4',
]
all_folders = [
'Game Throne',
'Other Files'
]
for filename in all_filenames:
words = filename.lower().split(' ')
matching = []
for folder in all_folders:
count = 0
for word in words:
if word in folder.lower():
count += 1
if count >= 2:
print('count:', count, '|', filename, '->', folder)
matching.append(folder)
#if not matching:
if len(matching) == 0:
print('not found folder for:', filename)
# TODO: you could move file to `Other Files`
elif len(matching) == 1:
print('move:', filename, '->', matching[0])
# TODO: move file to folder matching[0]
else:
for number, item in enumerate(matching):
print(number, item)
answer = int(input('choose number:'))
print('move:', filename, '->', matching[answer])
# TODO: move file to folder matching[answer]