Python:[错误3]系统找不到指定的路径:
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python:[错误3]系统找不到指定的路径:,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2830字,纯文字阅读大概需要5分钟。
内容图文
![Python:[错误3]系统找不到指定的路径:](/upload/InfoBanner/zyjiaocheng/772/46fa14a800ff4d78a668963920bb923c.jpg)
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:\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'
解决方法:
你必须在Windows机器上.错误是因为os.listdir(). os.listdir()没有获得正确的路径.
在第3行,你正在做os.path.join(“archive”,目录).
你应该加入完整的路径,包括驱动器(C:或D :),如“c:/ archive / foo:或在linux上
“家用/根/存档/富”
阅读 – Python os.path.join on Windows
On Windows, the drive letter is not reset when an absolute path
component (e.g., r’\foo’) is encountered. If a component contains a
drive letter, all previous components are thrown away and the drive
letter is reset. Note that since there is a current directory for each
drive, os.path.join(“c:”, “foo”) represents a path relative to the
current directory on drive C: (c:foo), not c:\foo.
编辑:
您将列表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:archive\foo1
['d:archive\\foo1\\foo1.txt.txt', 'd:archive\\foo1\\foo11.txt']
d:archive\foo2
['d:archive\\foo2\\foo2.txt.txt']
d:archive\foo3
['d:archive\\foo3\\foo3.txt.txt']
内容总结
以上是互联网集市为您收集整理的Python:[错误3]系统找不到指定的路径:全部内容,希望文章能够帮你解决Python:[错误3]系统找不到指定的路径:所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。