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

  • Chriselle Lim

    Chriselle Li

    26 Ocak 2008
  • Easy Learn Tutorial

    Easy Learn T

    10 Kasım 2012
  • уσ ρℓz sυв ιℓℓ sυв вαcқ

    уσ ρℓz

    14 EKİM 2010