Python:[错误3]系统找不到指定的路径:
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的文件中运行此代码,并且在存档中有更多文件夹,并且在每个文件夹中,都有一个或多个文本文件.我想创建一个列表,其中包含每个文件夹的路径.但是会出现以下错误. [Error 3] The system cannot find the path specified: 我目前有python脚本,我在与归档相同的文件夹中编写此代码,它将触发此错误.我该怎么做才能阻止此错误并获取所有文件路径. 我使用操作系统非常糟糕,我不经常使用它,所以如果这是一个微不足道的问题,我道歉. 编辑 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:UsersDavidAnacondalibntpath.py",line 65,in join result_drive,result_path = splitdrive(path) File "C:UsersDavidAnacondalibntpath.py",line 116,in splitdrive normp = p.replace(altsep,sep) AttributeError: 'list' object has no attribute 'replace' 解决方法
你必须在Windows机器上.错误是因为os.listdir(). os.listdir()没有获得正确的路径.
在第3行,你正在做os.path.join(“archive”,目录). 阅读 – Python os.path.join on Windows os.path.join Usage –
编辑: 您将列表corpus_path传递给第6行中的[os.path.join] [2].这会导致错误!用项目替换corpus_path. 我在’D:’驱动器中创建了存档文件夹.在存档文件夹下,我创建了3个文件夹foo1,foo2和foo3.每个文件夹包含1或2个文本文件.然后我在修改后测试了你的代码.代码工作正常. import os startpath = "d:archive" corpus_path = sorted([os.path.join("d:","archive",directories) for directories in os.listdir(startpath)]) filenames = [] for items in corpus_path: print items path = [os.path.join(items,fn) for fn in os.listdir(items)] print path 输出: d:archivefoo1 ['d:archivefoo1foo1.txt.txt','d:archivefoo1foo11.txt'] d:archivefoo2 ['d:archivefoo2foo2.txt.txt'] d:archivefoo3 ['d:archivefoo3foo3.txt.txt'] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |