SORU
23 EYLÜL 2008, Salı


Python ile dizin listeleme

Nasıl Python belirli bir dizindeki tüm dosyaları (ve dizinler) bir listesini alabilir miyim?

CEVAP
23 EYLÜL 2008, Salı


Bu geçiş her dosya ve dizin ağacında dizin için bir yoldur:

import os

for dirname, dirnames, filenames in os.walk('.'):
    # print path to all subdirectories first.
    for subdirname in dirnames:
        print(os.path.join(dirname, subdirname))

    # print path to all filenames.
    for filename in filenames:
        print(os.path.join(dirname, filename))

    # Advanced usage:
    # editing the 'dirnames' list will stop os.walk() from recursing into there.
    if '.git' in dirnames:
        # don't go into any .git directories.
        dirnames.remove('.git')

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CNNMoney

    CNNMoney

    16 Kasım 2006
  • spederson7

    spederson7

    17 Temmuz 2006
  • Thom Hall

    Thom Hall

    24 Kasım 2006