Python Forum
select files such as text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: select files such as text file (/thread-37560.html)



select files such as text file - RolanRoll - Jun-25-2022

Hi I want to find and select files that has the same word in my text list for example choose and select
S1A_OPER_AUX_POEORB_OPOD_20190122T120724_V20190101T225942_20190103T005942.EOF
In folder:
because I have the V20190101 in my text file that is like this:
note: it should be same and match exactly with all of the word (V20190101) not just 20190101

V20220103
V20190101
V20220405
V20211215
V20220111

and save them in another place ( for example new folder2)


RE: select files such as text file - BashBedlam - Jun-25-2022

This is how I would do it:
import os
import shutil

with open ("pattern_list.txt") as pattern_list_file :
	pattern_data = pattern_list_file.read ()
pattern_list = pattern_data.split ('\n') [:-1]

file_name_list = [file_name for file_name in os.listdir ("old_dir")]

for file_name in file_name_list :
	for pattern in pattern_list :
		if pattern in file_name :
			shutil.move ("old_dir/" + file_name, "new_dir/"+ file_name)



RE: select files such as text file - RolanRoll - Jun-25-2022

thank you