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

  • AndroidSale

    AndroidSale

    17 NİSAN 2011
  • DragsterMC Gaming

    DragsterMC G

    30 HAZİRAN 2013
  • Munchkin the Teddy Bear

    Munchkin the

    30 EYLÜL 2011