Python Forum

Full Version: select files such as text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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)
thank you