我正在尝试列出当前文件夹中的所有文件以及当前文件夹的文件夹中的文件。 这就是我一直在做的:
import os
def sendFnF(dirList):
for file in dirList:
if os.path.isdir(file):
print 'Going in dir:',file
dirList1= os.listdir('./'+file)
# print 'files in list', dirList1
sendFnF(dirList1)
print 'backToPrevDirectory:'
else:
print 'file name is',file
filename= raw_input()
dirList= os.listdir('./'+filename)
sendFnF(dirList)
这段代码确实让我进入当前目录的文件夹。但当涉及到子文件夹时,它会将它们视为文件。 知道我做错了什么吗? 提前谢谢, 中士。你知道吗
在路径前面加
./
基本上没有什么作用。而且,仅仅因为使用目录路径递归调用函数,并不会更改当前目录,因此也不会更改文件路径中.
的含义。你知道吗您的基本方法是正确的,使用
os.path.join()
进入目录。最好重新构造代码,以便在sendFnF()
开始时listdir()
:也就是说,除非这是一个练习,否则您可以使用^{}
相关问题 更多 >
编程相关推荐