Python:从具有给定名称列表的目录中选择文件
Python: Selecting files from a directory with a given list of names
基本上,我需要根据文件名从目录中挑选一些文件并将它们移动到其他地方。
所以,我有一个文件名列表,我需要执行以下操作:
1)逐行读取.txt文件
2)将列表中的字符串与给定目录的文件names/paths进行比较
3)将匹配的移动到新目录。
List_1
Out:
['/path_to_the_files/OG0000005.fa',
'/path_to_the_files/OG0000010.fa',
'/path_to_the_files/OG0000015.fa',
'/path_to_the_files/OG0000020.fa',
'/path_to_the_files/OG0000025.fa',
'/path_to_the_files/OG0000030.fa']
List_2
Out:
['OG0000010.fa',
'OG0000020.fa',
'OG0000030.fa']
import re
import shutil
def filter_list(string, substr):
return [st for st in string if any(sub in st for sub in substr)]
#List_1, files in a given directory
list_1=glob.glob('/path_to_the_files/*.fa')
#List_2, files that need to be picked
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n")
filtered_list=filter_list(list_2,list_1)
shutil.("old_path_to_the_filtered_list",
"new_path_to_the_filtered_list")
感谢任何帮助!
您只是想弄清楚如何在过滤后将文件移动到新目录,对吗?只需在您的路径上调用 str.replace
即可生成新路径,然后将这些字符串传递给 shutil.move
.
import re
import shutil
import glob
def filter_list(string, substr):
return [st for st in string if any(sub in st for sub in substr)]
#List_1, files in a given directory
old_path = "/path_to_the_files"
new_path = "/path_to_move_files_to"
list_1=glob.glob(f'{old_path}/*.fa')
#List_2, files that need to be picked
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n")
filtered_list=filter_list(list_2,list_1)
for input in filtered_list:
output = input.replace(old_path, new_path)
shutil.move(input, output)
假设这是您阅读后的清单 list_2.txt
['OG0000010.fa',
'OG0000020.fa',
'OG0000030.fa']
然后你可以循环浏览文件所在的目录,如果你的文件在列表中,则将它们移动到新目录,
import shutil
import os
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n") # ['OG0000010.fa','OG0000020.fa','OG0000030.fa']
directory = 'path/to/file/'
new_directory = 'path/to/new/dir'
for filename in os.listdir(directory):
if filename in list_2:
old_loc = rf"{directory}/{filename}"
new_loc = rf"{new_directory}/{filename}"
shutil.move(old_loc, new_loc)
基本上,我需要根据文件名从目录中挑选一些文件并将它们移动到其他地方。
所以,我有一个文件名列表,我需要执行以下操作:
1)逐行读取.txt文件
2)将列表中的字符串与给定目录的文件names/paths进行比较
3)将匹配的移动到新目录。
List_1
Out:
['/path_to_the_files/OG0000005.fa',
'/path_to_the_files/OG0000010.fa',
'/path_to_the_files/OG0000015.fa',
'/path_to_the_files/OG0000020.fa',
'/path_to_the_files/OG0000025.fa',
'/path_to_the_files/OG0000030.fa']
List_2
Out:
['OG0000010.fa',
'OG0000020.fa',
'OG0000030.fa']
import re
import shutil
def filter_list(string, substr):
return [st for st in string if any(sub in st for sub in substr)]
#List_1, files in a given directory
list_1=glob.glob('/path_to_the_files/*.fa')
#List_2, files that need to be picked
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n")
filtered_list=filter_list(list_2,list_1)
shutil.("old_path_to_the_filtered_list",
"new_path_to_the_filtered_list")
感谢任何帮助!
您只是想弄清楚如何在过滤后将文件移动到新目录,对吗?只需在您的路径上调用 str.replace
即可生成新路径,然后将这些字符串传递给 shutil.move
.
import re
import shutil
import glob
def filter_list(string, substr):
return [st for st in string if any(sub in st for sub in substr)]
#List_1, files in a given directory
old_path = "/path_to_the_files"
new_path = "/path_to_move_files_to"
list_1=glob.glob(f'{old_path}/*.fa')
#List_2, files that need to be picked
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n")
filtered_list=filter_list(list_2,list_1)
for input in filtered_list:
output = input.replace(old_path, new_path)
shutil.move(input, output)
假设这是您阅读后的清单 list_2.txt
['OG0000010.fa',
'OG0000020.fa',
'OG0000030.fa']
然后你可以循环浏览文件所在的目录,如果你的文件在列表中,则将它们移动到新目录,
import shutil
import os
file = open("/path_to_name_list/list_2.txt", "r")
file_lines = file.read()
list_2 = file_lines.split("\n") # ['OG0000010.fa','OG0000020.fa','OG0000030.fa']
directory = 'path/to/file/'
new_directory = 'path/to/new/dir'
for filename in os.listdir(directory):
if filename in list_2:
old_loc = rf"{directory}/{filename}"
new_loc = rf"{new_directory}/{filename}"
shutil.move(old_loc, new_loc)