ADMMTL
ADMMTL

Reputation: 43

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']

Upvotes: 3

Views: 8052

Answers (6)

pink.slash
pink.slash

Reputation: 1997

Filter with glob module:

Import glob

import glob

Pattern

pattern='*High_Quality*'

files=glob.glob(pattern)

Wild Cards:

files=glob.glob("data/*")

print(files)


Out:

['data/ks_10000_0', 'data/ks_1000_0', 'data/ks_100_0', 'data/ks_100_1',
'data/ks_100_2', 'data/ks_106_0', 'data/ks_19_0', 'data/ks_200_0', 'data/ks_200_1', 
'data/ks_300_0', 'data/ks_30_0', 'data/ks_400_0', 'data/ks_40_0', 'data/ks_45_0', 
'data/ks_4_0', 'data/ks_500_0', 'data/ks_50_0', 'data/ks_50_1', 'data/ks_60_0', 
'data/ks_82_0', 'data/ks_lecture_dp_1', 'data/ks_lecture_dp_2']

Fiter extension .txt:

files = glob.glob("/home/ach/*/*.txt")

A single character

glob.glob("/home/ach/file?.txt")

Number Ranges

glob.glob("/home/ach/*[0-9]*")

Alphabet Ranges

glob.glob("/home/ach/[a-c]*")

Upvotes: 0

boot-scootin
boot-scootin

Reputation: 12515

Another approach using filter:

high_quality = filter(lambda fname: 'High_Quality' in fname, os.listdir('myPath'))

Convert high_quality to a list or set if you want to iterate over it more than once (filter returns a generator).

Upvotes: 1

Iron Fist
Iron Fist

Reputation: 10961

You can use filter method from fnmatch module for the same purpose, very straightforward application, just make sure you build the correct pattern to your needs, like in your case matching all string including High_Quality :

>>> l = ['test_Draft.txt', 'test_Mid_Quality.txt', 'test_High_Quality.txt']
>>> 
>>> import fnmatch
>>> 
>>> fnmatch.filter(l, "*High_Quality*")
['test_High_Quality.txt']

Upvotes: 1

Wayne Werner
Wayne Werner

Reputation: 51907

If you stick this file in an empty directory you can see how glob will work just fine for you. You just need *High_Quality* as your glob pattern:

from __future__ import print_function

import glob
import os


filenames = [
    'fnord.txt',
    'fizzy.txt',
    'test_Low_Quality.txt',
    'test_Mid_Quality.txt',
    'test_High_Quality.txt',
    'test_High_Quality_one.txt',
    'test_High_Quality_two.txt',
]

for filename in filenames:
    with open(filename, 'w'): pass
print('Files:')
print('\t', '\n\t'.join(os.listdir(os.curdir)), sep='')
print('Files matching *High_Quality*:')
print('\t', '\n\t'.join(glob.glob('*High_Quality*')), sep='')

Upvotes: 0

manvi77
manvi77

Reputation: 570

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

Upvotes: 3

salezica
salezica

Reputation: 77099

You could use the glob.glob() function to search for filenames matching patterns.

> from glob import glob
> glob("C:\Project\*High_Quality*")

['test_High_Quality.txt']

See the linked documentation for further details.

Upvotes: 1

Related Questions