有一个目录树结构,其中article是主文件夹,d10x是子文件夹
/articles
/d101
1.txt 2.txt 3.txt
/d102
1.txt 2.txt 3.txt
/d103
1.txt 2.txt 3.txt
/d104
.....
.....
.....
/d200
我的程序必须转到每个子文件夹并从中读取文本文件。
我的 python 实现出现以下错误
操作系统错误:[Errno 2]没有这样的文件或目录:'d101'
Python代码:
articles = os.listdir("articles")
mo=[]
for yo in articles:
mo.append(yo)
for index in range(len(mo)):
yo=os.listdir(mo[index])
for article in yo:
print 'Reading articles/' + article
操作系统错误:[Errno 2] 没有这样的文件或目录:“d101”
最佳答案
您正在尝试读取 d101
而不是 articles/d101
。将您的 listdir()
调用更改为:
os.listdir(os.path.join("articles", mo[index]))
关于python - 在 python 中导航子目录时找不到 OSError 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35627512/