SORU
19 EKİM 2010, Salı


Uzantısı ile dizindeki tüm dosyaları bulabilirsiniz .txt python ile

Nasıl uzantısı ile dizindeki tüm dosyaları bulabilirim .python txt?

CEVAP
19 EKİM 2010, Salı


glob kullanabilirsiniz:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)

ya da sadece os.listdir:

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        print(file)

veya çapraz dizin etmek istiyorsanız, os.walk kullanın:

import os
for root, dirs, files in os.walk("/mydir"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • B3ASTTY™

    B3ASTTY™

    27 Mayıs 2013
  • bored before i even began

    bored before

    30 Mart 2009
  • TopDJMag TV

    TopDJMag TV

    29 Temmuz 2010