python 文件目录遍历代码 输出不对
def listAppendlist(l1,l2):
    for x in l2:
        l1.append(x);
def mylistAllFiles(pathstr):
    pathfiles = [];
    pathfilestmp = os.listdir(pathstr);
    for x in pathfilestmp:
        pathfiles.append(os.path.abspath(x));
    print(pathfiles);
    for x in pathfiles:
        print(x);
        if os.path.isdir(x):
            listAppendlist(pathfiles,mylistAllFiles(x));
    return pathfiles;
lfiles =  mylistAllFiles('D:\pythonWorkSpace')

实际目录结构如下:
D:\pythonWorkSpace\Demo.py
D:\pythonWorkSpace\DemoPro
D:\pythonWorkSpace\DemoPro.idea
D:\pythonWorkSpace\DemoPro.idea\DemoPro.iml
D:\pythonWorkSpace\DemoPro.idea\encodings.xml
D:\pythonWorkSpace\DemoPro.idea\misc.xml
D:\pythonWorkSpace\DemoPro.idea\modules.xml
D:\pythonWorkSpace\DemoPro.idea\workspace.xml
D:\pythonWorkSpace\DemoPro\com
D:\pythonWorkSpace\DemoPro\com\aruforce
D:\pythonWorkSpace\DemoPro\com\aruforce\demo
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo2.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\DocTestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\GcDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\IODemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\UnitestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__pycache__
D:\pythonWorkSpace\DemoPro\com\aruforce__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce__pycache__
D:\pythonWorkSpace\DemoPro\com__init__.py
D:\pythonWorkSpace\DemoPro\com__pycache__
D:\pythonWorkSpace\HelloWorld.py
D:\pythonWorkSpace\test.jpg
D:\pythonWorkSpace\test2.jpg

lfiles 输出为:
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\DemoPro
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\HelloWorld.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\test.jpg
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\test2.jpg
指数输出了一级目录

请帮满看看代码问题出在哪里?

3个回答

解决了 os.path.isdir(x) 的问题。

 import os;# 操作系统lib
def listAppendlist(l1,l2):
    for x in l2:
        l1.append(x);
def mylistAllFiles(pathstr):
    pathfiles = os.listdir(pathstr);
    allfile = [];
    for x in pathfiles:
        filepath = os.path.join(pathstr,x)
        #print(filepath,os.path.isdir(filepath));
        allfile.append(filepath);
        if os.path.isdir(filepath):
            listAppendlist(allfile,mylistAllFiles(filepath));
    return allfile;
allfiles = mylistAllFiles('D:\pythonWorkSpace')
for x in allfiles:
    print(x);

实际目录结构如下:
D:\pythonWorkSpace\Demo.py
D:\pythonWorkSpace\DemoPro
D:\pythonWorkSpace\DemoPro.idea
D:\pythonWorkSpace\DemoPro.idea\DemoPro.iml
D:\pythonWorkSpace\DemoPro.idea\encodings.xml
D:\pythonWorkSpace\DemoPro.idea\misc.xml
D:\pythonWorkSpace\DemoPro.idea\modules.xml
D:\pythonWorkSpace\DemoPro.idea\workspace.xml
D:\pythonWorkSpace\DemoPro\com
D:\pythonWorkSpace\DemoPro\com\aruforce
D:\pythonWorkSpace\DemoPro\com\aruforce\demo
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo2.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\DocTestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\GcDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\IODemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\UnitestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__pycache__
D:\pythonWorkSpace\DemoPro\com\aruforce__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce__pycache__
D:\pythonWorkSpace\DemoPro\com__init__.py
D:\pythonWorkSpace\DemoPro\com__pycache__
D:\pythonWorkSpace\HelloWorld.py
D:\pythonWorkSpace\test.jpg
D:\pythonWorkSpace\test2.jpg

程序输出为:
D:\pythonWorkSpace\Demo.py
D:\pythonWorkSpace\DemoPro
D:\pythonWorkSpace\DemoPro.idea
D:\pythonWorkSpace\DemoPro.idea\DemoPro.iml
D:\pythonWorkSpace\DemoPro.idea\encodings.xml
D:\pythonWorkSpace\DemoPro.idea\misc.xml
D:\pythonWorkSpace\DemoPro.idea\modules.xml
D:\pythonWorkSpace\DemoPro.idea\workspace.xml
D:\pythonWorkSpace\DemoPro\com
D:\pythonWorkSpace\DemoPro\com\aruforce
D:\pythonWorkSpace\DemoPro\com\aruforce\demo
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\Demo2.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\DocTestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\GcDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\IODemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo\UnitestDemo.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce\demo__pycache__
D:\pythonWorkSpace\DemoPro\com\aruforce__init__.py
D:\pythonWorkSpace\DemoPro\com\aruforce__pycache__
D:\pythonWorkSpace\DemoPro\com__init__.py
D:\pythonWorkSpace\DemoPro\com__pycache__
D:\pythonWorkSpace\HelloWorld.py
D:\pythonWorkSpace\test.jpg
D:\pythonWorkSpace\test2.jpg

call_me_idiot
call_me_idiot pycharm真难用。。。
接近 3 年之前 回复

pathfiles.append(os.path.abspath(x));
分号是全角的

call_me_idiot
call_me_idiot 只是粘贴错了 代码里面是 英文的 我 debug时 发现 os.path.isdir(x) 对于文件夹判断是False ,不论 x 只是文件夹 还是全路径
接近 3 年之前 回复
call_me_idiot
call_me_idiot 你能帮看看我的代码错误在那里么?
接近 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问