2013-03-09 12 views

cevap

22

os.listdir, os.path.join ve os.path.isfile kullanmayı deneyin. Liste-comprehensions ile (döngüler için birlikte) uzun formda
,

import os 
path = 'C:/' 
files = [] 
for i in os.listdir(path): 
    if os.path.isfile(os.path.join(path,i)) and '001_MN_DX' in i: 
     files.append(i) 

Kod, uzun açıklama için

import os 
path = 'C:/' 
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \ 
     '001_MN_DX' in i] 

Kontrol here ... olduğunu

+0

ile başladığı geçerli dizindeki tüm dosyaları bulun. Teşekkürler. :) –

+5

've' 001_MN_DX 'i '' ve i.startswith (' 001_MN_DX ') ' ile değiştiririm' E_001_MN_DX.txt 'dosyası orijinal kodla eşleşeceği için. – karelv

5
import os, re 
for f in os.listdir('.'): 
    if re.match('001_MN_DX', f): 
     print f 
3

yapabilirsiniz Bir dizindeki dosyaları listelemek için os modülünü kullanın.

Ör: adı Bu iyi çalışıyor 001_MN_DX

import os 
list_of_files = os.listdir(os.getcwd()) #list of files in the current directory 
for each_file in list_of_files: 
    if each_file.startswith('001_MN_DX'): #since its all type str you can simply use startswith 
     print each_file 
24
import os 
prefixed = [filename for filename in os.listdir('.') if filename.startswith("prefix")] 
İlgili konular