【发布时间】:2015-11-28 17:33:22
【问题描述】:
import os
Current_Directory = os.getcwd() # Should be ...\archive
CORPUS_PATHS = sorted([os.path.join("archive", directories) for directories in os.listdir(Current_Directory)])
filenames = []
for items in CORPUS_PATHS:
filenames.append(sorted([os.path.join(CORPUS_PATHS, fn) for fn in os.listdir(items)]))
print filenames
我正在从一个名为 archive 的文件中运行此代码,并且在 archive 中有更多文件夹,并且在每个文件夹中都有一个或多个文本文件。我想制作一个列表,其中包含每个文件夹的路径。但是出现以下错误。
[Error 3] The system cannot find the path specified:
我目前有一个 python 脚本,我在与存档相同的文件夹中编写了这段代码,它会触发这个错误。我应该怎么做才能停止此错误并获取所有文件路径。
我不擅长使用 os,而且我不经常使用它,所以如果这是一个微不足道的问题,我深表歉意。
编辑
import os
startpath = "archive"
corpus_path = sorted([os.path.join("archive/", directories) for directories in os.listdir(startpath)])
filenames = []
for items in corpus_path:
print items
path = [os.path.join(corpus_path, fn) for fn in os.listdir(items)]
print path
所以我已经取得了一些进展,现在我的语料库路径本质上是一个列表,其中包含所有所需文件夹的路径。现在我要做的就是获取这些文件夹中文本文件的所有路径,但我仍然遇到问题,我不知道如何但错误,例如
File "C:\Users\David\Anaconda\lib\ntpath.py", line 65, in join
result_drive, result_path = splitdrive(path)
File "C:\Users\David\Anaconda\lib\ntpath.py", line 116, in splitdrive
normp = p.replace(altsep, sep)
AttributeError: 'list' object has no attribute 'replace'
【问题讨论】:
-
查看答案here
标签: python windows operating-system