首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python目录遍历os.path

Python目录遍历os.path
EN

Stack Overflow用户
提问于 2013-04-13 01:08:59
回答 2查看 953关注 0票数 0

我尝试创建一个程序,它可以递归地遍历多个目录,并以层次结构的方式打印文件列表,如下所示:

代码语言:javascript
复制
Folder
----x.c
----x.bin
----Folder
---------x.c

我尝试使用类似于(包含文件/文件夹详细信息)的程序:

代码语言:javascript
复制
#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item) :
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing \n"

但我不能进入我尝试过的任何目录(这里的A是一个目录):

代码语言:javascript
复制
#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item):
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing"

for item in os.listdir("A") :
        if os.path.isdir("A") :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)

        elif os.path.isfile("A") :
                print "--" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing"

这个列表是错的,我不明白为什么我不能直接离开。to A和how to do it .And worst if I go B(这里的第二个文件夹):

代码语言:javascript
复制
#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item):
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "dunno"

for item in os.listdir("A") :
        if os.path.isdir("A") :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)

        elif os.path.isfile("A") :
                print "--" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "lulz"

for item in os.listdir("A/B") :
        if os.path.isfile("A/B") :
                print "---" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print 'Nothing'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-04-13 01:15:41

我认为你想使用os.walk

代码语言:javascript
复制
for (cur, dirs, files) in os.walk('.'):
    pass

这将为您提供当前目录、当前目录中的目录列表和当前目录中的文件列表。

我想你想要像这样的

代码语言:javascript
复制
for (cur, dirs, files) in os.walk('.'):
    depth = len(cur.split('/'))
    print "--" * depth, cur
    for fname in files:
        print "--" * (depth + 1), fname
票数 1
EN

Stack Overflow用户

发布于 2013-04-13 01:26:00

从这个答案中借用了一点:List directory tree structure using Python

代码语言:javascript
复制
import os

def list_files(path, spaceChar=' ', spaceWidth=4):
    for root, dirs, files in os.walk(path):
        level = root.replace(path, '').count(os.sep)
        indent = spaceChar * (spaceWidth * level)
        print('{}{}/'.format(indent, os.path.basename(root)))
        subindent = spaceChar * spaceWidth * (level + 1)
        for f in files:
            print('{}{}'.format(subindent, f))

list_files(".", "-", 3)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15977133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档