Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 - Filter files with a specific string in filename inside a directory

I'd like to move a file from one directory to another by filtering the filename with a specific string

Seems like fnmatch or glob can do this but I can't figure it out

In the example below, how could python move only the file test_High_Quality.mb to another directory using the filter High_Quality in the filename

>>> import os    
>>> myPath = "C:\Project"    
>>> os.listdir('myPath')    
>>> ['test_Draft.txt', 'test_Mid_Quality.txt', 'test_High_Quality.txt']
like image 319
ADMMTL Avatar asked Jul 22 '26 05:07

ADMMTL


1 Answers

To filter with file names "High_Quality"

[d for d in os.listdir('myPath') if 'High_Quality' in d]

For moving to other directory, follow this solution similar question

like image 68
manvi77 Avatar answered Jul 24 '26 20:07

manvi77



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!