Python Forum
select files such as text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
select files such as text file
#1
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)
Reply
#2
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)
RolanRoll likes this post
Reply
#3
thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,776 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,173 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing into 2 text files from the same function paul18fr 4 1,749 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,641 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  Two text files, want to add a column value zxcv101 8 2,007 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,761 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,566 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Converted Pipe Delimited text file to CSV file atomxkai 4 7,125 Feb-11-2022, 12:38 AM
Last Post: atomxkai
  Separate text files and convert into csv marfer 6 2,989 Dec-10-2021, 12:09 PM
Last Post: marfer
  Sorting and Merging text-files [SOLVED] AlphaInc 10 5,028 Aug-20-2021, 05:42 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020