regex - Find a file in a directory using python by partial name -


i have directory several hundred thousand files in it.

they follow format:

datetime_fileid_metadata_collect.txt 

a specific example looks :

201405052359559_0002230255_35702088_collect88.txt 

i trying write script pulls out , copies individual files when provide list of file ids.

for example have text document fileids.txt constains this

fileids.txt 0002230255 0001627237 0001023000 

this example script have written far. file1 result keeps returning []

import os import re, glob, shutil base_dir = 'c:/stuff/tub_0_data/' destination = 'c:/files_goes_here' os.chdir(base_dir) text_file = open('c:/stuff/fileids.txt', 'r') file_ids = text_file.readlines() #file_ids = [stripped stripped in (line.strip() line in text_file.readlines()) if stripped] ids in file_ids:     id1 = ids.rstrip()     print 'file id = ',str(id1)     file1 = glob.glob('*' + str(id1) + '*')     print str(file1)     if file1 != []:         shutil.copy(base_dir + file1, destination) 

i know dont understand glob or regular expressions yet. put there if want find files based off of specific string of filename?

edit:

glob.glob('*' + stuff '*')  

worked finding things within filename. not removing linespace issue.

text_file.readlines() reads entire line including trailing '\n'. try stripping it. following strip newlines , remove empties:

file_ids = [line.strip() line in text_file if not line.isspace()] 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -