打印每个目录python的文件数

时间:2014-12-09 06:51:17

标签: python

我在这个主题上发现了几个问题,但没有一个问题解决了我所看到的问题。我提前道歉我是python的新手。我想要做的是计算somedir下每个目录中的文件数量/所以我会得到类似的东西:

dir-a: 13
dir-b: 6
dir-c: 21

我认为证明我能做到这一点的最简单方法是:

from os import listdir
from os.path import isfile, isdir

def walk(p):
  for file in listdir(p):
    if isfile(file):
      print("File: " + file)
    elif isdir(file):
      print("Dir: " + file)
      walk(file)
    else:
      print("Unknown: " + file)

path = raw_input('Enter path to search')
walk(path)

但是第一次迭代是唯一一个按预期工作的迭代。我尝试使用os.path.abspath添加文件的完整路径以及其他几种方法来确保文件在第一次迭代中与递归调用中的文件相同。我已使用type(file)检查了类型,它是'str',即使路径+文件名正确且存在,上述程序仍无法产生预期结果。例如,在目录结构上:

dir-a/
     file-a.1
     file-a.2
     dir-b/
          file-b.1
          file-b.2
     file-a.3

我希望:

File: file-a.1
File: file-a.2
Dir: dir-b
File: file-b.1
File: file-b.2
File: file-a.3

但我明白了:

File: file-a.1
File: file-a.2
Dir: dir-b
Unknown: file-b.1
Unknown: file-b.2
File: file-a.3

所以基本上所有文件都没有在递归调用中被识别为文件或目录。我查看了listdirisfileisdir上的文档,但仍然没有意义。我尝试过使用python 2.7.5和3.4.0,同样的行为。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:4)

你对绝对路径的看法是正确的。您可以通过简单地将listdir的内容加到传递给walk

的变量上来自己构建路径
from os import listdir
from os.path import isfile, isdir, join

def walk(p):
  for item in listdir(p):
    item = join(p, item)
    if isfile(item):
      print("File: " + item)
    elif isdir(item):
      print("Dir: " + item)
      walk(item)
    else:
      print("Unknown: " + item)

path = raw_input('Enter path to search')
walk(path)

输出(注意:错误最终会导致我在此框中没有root,所以当它到达/root时会崩溃,但这很容易被捕获)

Enter path to search/
Dir: /tmp
Dir: /srv
Dir: /lib
Dir: /lib/systemd
Dir: /lib/systemd/system
File: /lib/systemd/system/dbus.socket
File: /lib/systemd/system/systemd-udev-settle.service
File: /lib/systemd/system/systemd-udev-trigger.service
File: /lib/systemd/system/dbus.service
File: /lib/systemd/system/ssh@.service
File: /lib/systemd/system/systemd-udevd-control.socket
Dir: /lib/systemd/system/multi-user.target.wants
File: /lib/systemd/system/multi-user.target.wants/dbus.service
File: /lib/systemd/system/rsync.service
File: /lib/systemd/system/rsyslog.service
File: /lib/systemd/system/sudo.service
File: /lib/systemd/system/systemd-udevd-kernel.socket
File: /lib/systemd/system/systemd-udevd.service
File: /lib/systemd/system/ssh.service
File: /lib/systemd/system/udev.service
File: /lib/systemd/system/ssh.socket
Dir: /lib/systemd/system/sysinit.target.wants
File: /lib/systemd/system/sysinit.target.wants/systemd-udev-trigger.service
File: /lib/systemd/system/sysinit.target.wants/systemd-udevd.service
File: /lib/systemd/system/beanstalkd.service
File: /lib/systemd/system/beanstalkd.socket
Dir: /lib/systemd/system/dbus.target.wants
File: /lib/systemd/system/dbus.target.wants/dbus.socket
Dir: /lib/systemd/system/sockets.target.wants
File: /lib/systemd/system/sockets.target.wants/dbus.socket
File: /lib/systemd/system/sockets.target.wants/systemd-udevd-control.socket
File: /lib/systemd/system/sockets.target.wants/systemd-udevd-kernel.socket
File: /lib/systemd/system/dnsmasq.service
File: /lib/systemd/systemd-udevd
Dir: /lib/terminfo
Dir: /lib/terminfo/E
File: /lib/terminfo/E/Eterm-color
File: /lib/terminfo/E/Eterm
Dir: /lib/terminfo/m
File: /lib/terminfo/m/mach-gnu-color
File: /lib/terminfo/m/mach-bold
File: /lib/terminfo/m/mach
File: /lib/terminfo/m/mach-color
File: /lib/terminfo/m/mach-gnu
Dir: /lib/terminfo/s
File: /lib/terminfo/s/screen-256color-bce
File: /lib/terminfo/s/screen-bce
File: /lib/terminfo/s/screen
File: /lib/terminfo/s/screen-w
File: /lib/terminfo/s/screen-s
File: /lib/terminfo/s/screen-256color
File: /lib/terminfo/s/sun
Dir: /lib/terminfo/h
File: /lib/terminfo/h/hurd
Dir: /lib/terminfo/c
File: /lib/terminfo/c/cons25
File: /lib/terminfo/c/cygwin
File: /lib/terminfo/c/cons25-debian
Dir: /lib/terminfo/v
File: /lib/terminfo/v/vt220
File: /lib/terminfo/v/vt102
File: /lib/terminfo/v/vt52
File: /lib/terminfo/v/vt100
Dir: /lib/terminfo/x
File: /lib/terminfo/x/xterm-vt220
File: /lib/terminfo/x/xterm
File: /lib/terminfo/x/xterm-mono
File: /lib/terminfo/x/xterm-debian
File: /lib/terminfo/x/xterm-xfree86
File: /lib/terminfo/x/xterm-color
File: /lib/terminfo/x/xterm-256color
File: /lib/terminfo/x/xterm-r5
File: /lib/terminfo/x/xterm-r6
Dir: /lib/terminfo/a
File: /lib/terminfo/a/ansi
Dir: /lib/terminfo/d
File: /lib/terminfo/d/dumb
Dir: /lib/terminfo/w
File: /lib/terminfo/w/wsvt25m
File: /lib/terminfo/w/wsvt25
Dir: /lib/terminfo/r
File: /lib/terminfo/r/rxvt-basic
File: /lib/terminfo/r/rxvt-m
File: /lib/terminfo/r/rxvt-unicode
File: /lib/terminfo/r/rxvt
Dir: /lib/terminfo/l
File: /lib/terminfo/l/linux
Dir: /lib/terminfo/p
File: /lib/terminfo/p/pcansi
Dir: /lib/lsb
File: /lib/lsb/init-functions
Dir: /lib/lsb/init-functions.d
File: /lib/lsb/init-functions.d/50-ubuntu-logging
File: /lib/lsb/init-functions.d/20-left-info-blocks
Dir: /lib/modules-load.d
File: /lib/modules-load.d/fuse.conf
Dir: /lib/init
File: /lib/init/apparmor-profile-load
File: /lib/init/upstart-job
File: /lib/init/fstab
File: /lib/init/vars.sh
File: /lib/klibc-P2s_k-gf23VtrGgO2_4pGkQgwMY.so
Dir: /lib/security
File: /lib/security/pam_chroot.so
File: /lib/security/pam_mysql.so
Dir: /lib/ifupdown
File: /lib/ifupdown/settle-dad.sh
File: /lib/cpp
Dir: /lib/udev
File: /lib/udev/write_net_rules
File: /lib/udev/collect
File: /lib/udev/accelerometer
File: /lib/udev/hotplug.functions
Dir: /lib/udev/rules.d
File: /lib/udev/rules.d/75-tty-description.rules
File: /lib/udev/rules.d/73-idrac.rules
File: /lib/udev/rules.d/75-persistent-net-generator.rules
File: /lib/udev/rules.d/64-btrfs.rules
File: /lib/udev/rules.d/60-persistent-v4l.rules
File: /lib/udev/rules.d/60-persistent-serial.rules
File: /lib/udev/rules.d/60-persistent-storage-dm.rules
File: /lib/udev/rules.d/85-keyboard-configuration.rules
File: /lib/udev/rules.d/78-graphics-card.rules
File: /lib/udev/rules.d/60-keyboard.rules
File: /lib/udev/rules.d/75-net-description.rules
File: /lib/udev/rules.d/40-gnupg.rules
File: /lib/udev/rules.d/40-hyperv-hotadd.rules
File: /lib/udev/rules.d/60-persistent-alsa.rules
File: /lib/udev/rules.d/60-persistent-storage.rules
File: /lib/udev/rules.d/78-sound-card.rules
File: /lib/udev/rules.d/70-power-switch.rules
File: /lib/udev/rules.d/61-accelerometer.rules
File: /lib/udev/rules.d/55-dm.rules
File: /lib/udev/rules.d/50-udev-default.rules
File: /lib/udev/rules.d/60-persistent-storage-tape.rules
File: /lib/udev/rules.d/60-persistent-input.rules
File: /lib/udev/rules.d/60-cdrom_id.rules
File: /lib/udev/rules.d/80-drivers.rules
File: /lib/udev/rules.d/42-usb-hid-pm.rules
File: /lib/udev/rules.d/75-probe_mtd.rules
File: /lib/udev/rules.d/README
File: /lib/udev/rules.d/50-firmware.rules
File: /lib/udev/rules.d/95-udev-late.rules
File: /lib/udev/scsi_id
Dir: /lib/udev/hwdb.d
File: /lib/udev/hwdb.d/20-pci-classes.hwdb
File: /lib/udev/hwdb.d/20-acpi-vendor.hwdb
File: /lib/udev/hwdb.d/20-pci-vendor-model.hwdb
File: /lib/udev/hwdb.d/20-bluetooth-vendor-product.hwdb
File: /lib/udev/hwdb.d/20-usb-vendor-model.hwdb
File: /lib/udev/hwdb.d/20-OUI.hwdb
File: /lib/udev/hwdb.d/20-usb-classes.hwdb
File: /lib/udev/hwdb.d/60-keyboard.hwdb
File: /lib/udev/ata_id
File: /lib/udev/rule_generator.functions
File: /lib/udev/v4l_id
File: /lib/udev/hwdb.bin
File: /lib/udev/cdrom_id
File: /lib/udev/mtd_probe
File: /lib/udev/console-setup-tty
Dir: /lib/plymouth
Dir: /lib/plymouth/themes
Dir: /lib/plymouth/themes/details
File: /lib/plymouth/themes/details/details.plymouth
File: /lib/plymouth/ubuntu_logo.png
Dir: /lib/modprobe.d
Dir: /lib/resolvconf
File: /lib/resolvconf/list-records
Dir: /lib/x86_64-linux-gnu
File: /lib/x86_64-linux-gnu/libjson.so.0
File: /lib/x86_64-linux-gnu/libdevmapper.so.1.02.1
File: /lib/x86_64-linux-gnu/liblzma.so.5.0.0
File: /lib/x86_64-linux-gnu/libacl.so.1
File: /lib/x86_64-linux-gnu/libtinfo.so.5
File: /lib/x86_64-linux-gnu/libpamc.so.0.82.1
File: /lib/x86_64-linux-gnu/libncursesw.so.5
File: /lib/x86_64-linux-gnu/libexpat.so.1.6.0
File: /lib/x86_64-linux-gnu/libulockmgr.so.1.0.1
File: /lib/x86_64-linux-gnu/libutil.so.1
File: /lib/x86_64-linux-gnu/libkeyutils.so.1
File: /lib/x86_64-linux-gnu/libbsd.so.0.6.0
File: /lib/x86_64-linux-gnu/libbz2.so.1
File: /lib/x86_64-linux-gnu/libply-splash-graphics.so.2.1.0
File: /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4
File: /lib/x86_64-linux-gnu/libselinux.so.1
File: /lib/x86_64-linux-gnu/libsystemd-login.so.0.7.1
File: /lib/x86_64-linux-gnu/libnss_files-2.19.so
File: /lib/x86_64-linux-gnu/libnss_hesiod.so.2
File: /lib/x86_64-linux-gnu/libhistory.so.6
File: /lib/x86_64-linux-gnu/libBrokenLocale-2.19.so
File: /lib/x86_64-linux-gnu/liblzma.so.5
File: /lib/x86_64-linux-gnu/liblzo2.so.2
File: /lib/x86_64-linux-gnu/libpopt.so.0
File: /lib/x86_64-linux-gnu/libncurses.so.5
File: /lib/x86_64-linux-gnu/libc-2.19.so
File: /lib/x86_64-linux-gnu/libbz2.so.1.0
File: /lib/x86_64-linux-gnu/libply.so.2
File: /lib/x86_64-linux-gnu/libfuse.so.2
File: /lib/x86_64-linux-gnu/libply-boot-client.so.2.1.0
File: /lib/x86_64-linux-gnu/libdbus-1.so.3
File: /lib/x86_64-linux-gnu/libply-splash-graphics.so.2
File: /lib/x86_64-linux-gnu/libBrokenLocale.so.1
File: /lib/x86_64-linux-gnu/libuuid.so.1
File: /lib/x86_64-linux-gnu/libnss_nis.so.2
File: /lib/x86_64-linux-gnu/libcgmanager.so.0
File: /lib/x86_64-linux-gnu/libSegFault.so
File: /lib/x86_64-linux-gnu/libnss_dns.so.2
File: /lib/x86_64-linux-gnu/libcgmanager.so.0.0.0
File: /lib/x86_64-linux-gnu/libnih-dbus.so.1
File: /lib/x86_64-linux-gnu/libnewt.so.0.52.15
File: /lib/x86_64-linux-gnu/libncurses.so.5.9
File: /lib/x86_64-linux-gnu/libblkid.so.1.1.0
File: /lib/x86_64-linux-gnu/libpthread.so.0
File: /lib/x86_64-linux-gnu/libpam_misc.so.0.82.0
File: /lib/x86_64-linux-gnu/libglib-2.0.so.0
File: /lib/x86_64-linux-gnu/libcidn-2.19.so
File: /lib/x86_64-linux-gnu/libgcc_s.so.1
File: /lib/x86_64-linux-gnu/libnss_nisplus-2.19.so
File: /lib/x86_64-linux-gnu/libpopt.so.0.0.0
File: /lib/x86_64-linux-gnu/libwrap.so.0
File: /lib/x86_64-linux-gnu/libpam_misc.so.0
File: /lib/x86_64-linux-gnu/libss.so.2
File: /lib/x86_64-linux-gnu/libcap.so.2.24
File: /lib/x86_64-linux-gnu/libply-splash-core.so.2
File: /lib/x86_64-linux-gnu/libkeyutils.so.1.4
File: /lib/x86_64-linux-gnu/libudev.so.1.3.5
File: /lib/x86_64-linux-gnu/libslang.so.2
File: /lib/x86_64-linux-gnu/libe2p.so.2.3
File: /lib/x86_64-linux-gnu/libmount.so.1.1.0
File: /lib/x86_64-linux-gnu/ld-2.19.so
File: /lib/x86_64-linux-gnu/libjson-c.so.2
File: /lib/x86_64-linux-gnu/libply.so.2.1.0
File: /lib/x86_64-linux-gnu/libply-splash-core.so.2.1.0
File: /lib/x86_64-linux-gnu/libuuid.so.1.3.0
File: /lib/x86_64-linux-gnu/libcrypt.so.1
File: /lib/x86_64-linux-gnu/libusb-1.0.so.0.1.0
File: /lib/x86_64-linux-gnu/libpcre.so.3
File: /lib/x86_64-linux-gnu/librt-2.19.so
File: /lib/x86_64-linux-gnu/libutil-2.19.so
File: /lib/x86_64-linux-gnu/libglib-2.0.so.0.4002.0
File: /lib/x86_64-linux-gnu/libusb-0.1.so.4
File: /lib/x86_64-linux-gnu/libncursesw.so.5.9
File: /lib/x86_64-linux-gnu/libaudit.so.1.0.0
File: /lib/x86_64-linux-gnu/libblkid.so.1
File: /lib/x86_64-linux-gnu/libcom_err.so.2
File: /lib/x86_64-linux-gnu/libmount.so.1
File: /lib/x86_64-linux-gnu/libbz2.so.1.0.4
File: /lib/x86_64-linux-gnu/libc.so.6
File: /lib/x86_64-linux-gnu/libnss_nis-2.19.so
File: /lib/x86_64-linux-gnu/libpamc.so.0
File: /lib/x86_64-linux-gnu/libm.so.6
File: /lib/x86_64-linux-gnu/libext2fs.so.2
File: /lib/x86_64-linux-gnu/libexpat.so.1
File: /lib/x86_64-linux-gnu/libpng12.so.0.50.0
File: /lib/x86_64-linux-gnu/libthread_db-1.0.so
File: /lib/x86_64-linux-gnu/libext2fs.so.2.4
File: /lib/x86_64-linux-gnu/libpam.so.0.83.1
File: /lib/x86_64-linux-gnu/libreadline.so.6.3
File: /lib/x86_64-linux-gnu/libanl.so.1
File: /lib/x86_64-linux-gnu/libe2p.so.2
File: /lib/x86_64-linux-gnu/libgpg-error.so.0
File: /lib/x86_64-linux-gnu/libresolv-2.19.so
File: /lib/x86_64-linux-gnu/libgcrypt.so.11
File: /lib/x86_64-linux-gnu/libnsl-2.19.so
File: /lib/x86_64-linux-gnu/liblzo2.so.2.0.0
File: /lib/x86_64-linux-gnu/libwrap.so.0.7.6
File: /lib/x86_64-linux-gnu/libdbus-1.so.3.7.6
File: /lib/x86_64-linux-gnu/libpng12.so.0
File: /lib/x86_64-linux-gnu/libmemusage.so
File: /lib/x86_64-linux-gnu/libacl.so.1.1.0
File: /lib/x86_64-linux-gnu/librt.so.1
File: /lib/x86_64-linux-gnu/libattr.so.1
File: /lib/x86_64-linux-gnu/libattr.so.1.1.0
File: /lib/x86_64-linux-gnu/libpcprofile.so
File: /lib/x86_64-linux-gnu/libudev.so.1
File: /lib/x86_64-linux-gnu/libnss_compat-2.19.so
File: /lib/x86_64-linux-gnu/libm-2.19.so
File: /lib/x86_64-linux-gnu/libnih.so.1.0.0
File: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
File: /lib/x86_64-linux-gnu/libply-boot-client.so.2
File: /lib/x86_64-linux-gnu/libulockmgr.so.1
File: /lib/x86_64-linux-gnu/libjson-c.so.2.0.0
Dir: /lib/x86_64-linux-gnu/security
File: /lib/x86_64-linux-gnu/security/pam_env.so
File: /lib/x86_64-linux-gnu/security/pam_stress.so
File: /lib/x86_64-linux-gnu/security/pam_succeed_if.so
File: /lib/x86_64-linux-gnu/security/pam_nologin.so
File: /lib/x86_64-linux-gnu/security/pam_localuser.so
File: /lib/x86_64-linux-gnu/security/pam_xauth.so
File: /lib/x86_64-linux-gnu/security/pam_tty_audit.so
File: /lib/x86_64-linux-gnu/security/pam_faildelay.so
File: /lib/x86_64-linux-gnu/security/pam_group.so
File: /lib/x86_64-linux-gnu/security/pam_shells.so
File: /lib/x86_64-linux-gnu/security/pam_warn.so
File: /lib/x86_64-linux-gnu/security/pam_selinux.so
File: /lib/x86_64-linux-gnu/security/pam_wheel.so
File: /lib/x86_64-linux-gnu/security/pam_issue.so
File: /lib/x86_64-linux-gnu/security/pam_umask.so
File: /lib/x86_64-linux-gnu/security/pam_debug.so
File: /lib/x86_64-linux-gnu/security/pam_limits.so
File: /lib/x86_64-linux-gnu/security/pam_mail.so
File: /lib/x86_64-linux-gnu/security/pam_securetty.so
File: /lib/x86_64-linux-gnu/security/pam_deny.so
File: /lib/x86_64-linux-gnu/security/pam_tally.so
File: /lib/x86_64-linux-gnu/security/pam_rhosts.so
File: /lib/x86_64-linux-gnu/security/pam_ftp.so
File: /lib/x86_64-linux-gnu/security/pam_keyinit.so
File: /lib/x86_64-linux-gnu/security/pam_loginuid.so
File: /lib/x86_64-linux-gnu/security/pam_lastlog.so
File: /lib/x86_64-linux-gnu/security/pam_userdb.so
File: /lib/x86_64-linux-gnu/security/pam_unix.so
File: /lib/x86_64-linux-gnu/security/pam_echo.so
File: /lib/x86_64-linux-gnu/security/pam_time.so
File: /lib/x86_64-linux-gnu/security/pam_rootok.so
File: /lib/x86_64-linux-gnu/security/pam_pwhistory.so
File: /lib/x86_64-linux-gnu/security/pam_listfile.so
File: /lib/x86_64-linux-gnu/security/pam_cap.so
File: /lib/x86_64-linux-gnu/security/pam_filter.so
File: /lib/x86_64-linux-gnu/security/pam_permit.so
File: /lib/x86_64-linux-gnu/security/pam_exec.so
File: /lib/x86_64-linux-gnu/security/pam_namespace.so
File: /lib/x86_64-linux-gnu/security/pam_mkhomedir.so
File: /lib/x86_64-linux-gnu/security/pam_tally2.so
File: /lib/x86_64-linux-gnu/security/pam_timestamp.so
File: /lib/x86_64-linux-gnu/security/pam_sepermit.so
File: /lib/x86_64-linux-gnu/security/pam_motd.so
File: /lib/x86_64-linux-gnu/security/pam_access.so
File: /lib/x86_64-linux-gnu/libnih-dbus.so.1.0.0
File: /lib/x86_64-linux-gnu/libcrypto.so.1.0.0
File: /lib/x86_64-linux-gnu/libgcrypt.so.11.8.2
File: /lib/x86_64-linux-gnu/libfuse.so.2.9.2
File: /lib/x86_64-linux-gnu/libprocps.so.3.0.0
File: /lib/x86_64-linux-gnu/libsystemd-login.so.0
File: /lib/x86_64-linux-gnu/libcidn.so.1
File: /lib/x86_64-linux-gnu/libcrypt-2.19.so
File: /lib/x86_64-linux-gnu/libanl-2.19.so
File: /lib/x86_64-linux-gnu/libnih.so.1
File: /lib/x86_64-linux-gnu/libnsl.so.1
File: /lib/x86_64-linux-gnu/libhistory.so.6.3
File: /lib/x86_64-linux-gnu/libpthread-2.19.so
File: /lib/x86_64-linux-gnu/libpam.so.0
File: /lib/x86_64-linux-gnu/libtinfo.so.5.9
File: /lib/x86_64-linux-gnu/libcom_err.so.2.1
File: /lib/x86_64-linux-gnu/libnss_dns-2.19.so
File: /lib/x86_64-linux-gnu/libgpg-error.so.0.10.0
File: /lib/x86_64-linux-gnu/libkmod.so.2
File: /lib/x86_64-linux-gnu/libnss_nisplus.so.2
File: /lib/x86_64-linux-gnu/libaudit.so.1
File: /lib/x86_64-linux-gnu/libdl.so.2
File: /lib/x86_64-linux-gnu/libcap.so.2
File: /lib/x86_64-linux-gnu/libslang.so.2.2.4
Dir: /lib/x86_64-linux-gnu/plymouth
File: /lib/x86_64-linux-gnu/plymouth/details.so
File: /lib/x86_64-linux-gnu/plymouth/script.so
Dir: /lib/x86_64-linux-gnu/plymouth/renderers
File: /lib/x86_64-linux-gnu/plymouth/renderers/vga16fb.so
File: /lib/x86_64-linux-gnu/plymouth/renderers/frame-buffer.so
File: /lib/x86_64-linux-gnu/plymouth/renderers/drm.so
File: /lib/x86_64-linux-gnu/libdl-2.19.so
File: /lib/x86_64-linux-gnu/libnewt.so.0.52
File: /lib/x86_64-linux-gnu/libssl.so.1.0.0
File: /lib/x86_64-linux-gnu/libprocps.so.3
File: /lib/x86_64-linux-gnu/libkmod.so.2.2.5
File: /lib/x86_64-linux-gnu/libsepol.so.1
File: /lib/x86_64-linux-gnu/libreadline.so.6
File: /lib/x86_64-linux-gnu/libnss_files.so.2
File: /lib/x86_64-linux-gnu/libthread_db.so.1
File: /lib/x86_64-linux-gnu/libnss_compat.so.2
File: /lib/x86_64-linux-gnu/libz.so.1
File: /lib/x86_64-linux-gnu/libss.so.2.0
File: /lib/x86_64-linux-gnu/libusb-1.0.so.0
File: /lib/x86_64-linux-gnu/libnss_hesiod-2.19.so
File: /lib/x86_64-linux-gnu/libpcre.so.3.13.1
File: /lib/x86_64-linux-gnu/libz.so.1.2.8
File: /lib/x86_64-linux-gnu/libbsd.so.0
File: /lib/x86_64-linux-gnu/libresolv.so.2
Dir: /mnt
Dir: /root
Traceback (most recent call last):
  File "/home/ngenator/StackOverflow/27373035.py", line 16, in <module>
    walk(path)
  File "/home/ngenator/StackOverflow/27373035.py", line 11, in walk
    walk(item)
  File "/home/ngenator/StackOverflow/27373035.py", line 5, in walk
    for item in listdir(p):
OSError: [Errno 13] Permission denied: '/root'

答案 1 :(得分:0)

您可以在Python中执行shell命令 - http://www.cyberciti.biz/faq/python-run-external-command-and-get-output/

Shell中的以下命令将列出目录中的文件数:

ls -1 targetdir | wc -l

答案 2 :(得分:0)

使用标准库中的walk的替代方法:

import os  
from collections import defaultdict

file_counter = defaultdict(list)

for root, dirs, files in os.walk("/tmp"):
    file_counter[root] += files


for k,v in file_counter.items():
    print(k, len(v))

打印:

/tmp/.felix/osgi-cache580844e_14a135fd513_-8000/bundle14 5
/tmp/.felix/osgi-cache71530794_14a2d25c3b4_-8000/bundle3/version0.0 1
/tmp/.felix/osgi-cache580844e_14a135fd513_-8000/bundle13 5
/tmp/.felix/osgi-cache580844e_14a135fd513_-8000/bundle1/version0.0 1
/tmp/.felix/osgi-cache71530794_14a2d25c3b4_-8000/bundle18 5
...

但缺点是它会存储所有文件名和方向的字典。这可能是昂贵的,您计划遍历大型文件系统。