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

  • Call Me Howard

    Call Me Howa

    18 AĞUSTOS 2012
  • Kevin Bruckert

    Kevin Brucke

    30 Aralık 2006
  • Philip DeFranco

    Philip DeFra

    16 EYLÜL 2006