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

  • Munchkin the Teddy Bear

    Munchkin the

    30 EYLÜL 2011
  • TheMasterOfHell100

    TheMasterOfH

    13 AĞUSTOS 2011
  • WK

    WK

    9 Ocak 2006