Python :[错误 3] 系统找不到指定的路径:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32366195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python :[Error 3] The system cannot find the path specified:
提问by David Yi
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
I am running this code from a file called archive and in archive there are more folders and in each of these folders, there are one or more text files. I want to make a list that includes the path to each of these folders. However the following error appears.
我从一个名为存档的文件中运行此代码,存档中有更多文件夹,在每个文件夹中,都有一个或多个文本文件。我想制作一个列表,其中包含每个文件夹的路径。但是出现以下错误。
[Error 3] The system cannot find the path specified:
I currently have the python script where I wrote this code in the same folder as archive and it will trigger this error. What should I do in order to stop this error and get all the file paths.
我目前有 python 脚本,我在与存档相同的文件夹中编写了此代码,它将触发此错误。我该怎么做才能停止此错误并获取所有文件路径。
I am pretty bad at using os and I don't use it that often so I apologize if this is a trivial question.
我不擅长使用 os,而且我不经常使用它,所以如果这是一个微不足道的问题,我深表歉意。
Edit
编辑
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
So I have made some progress and now I corpus path is essentially a list with the path to all of the desired folders. Now all I am trying to do is get all of the paths to the text files inside these folders but I still run into issues and I don't know how but error such as
所以我取得了一些进展,现在我的语料库路径本质上是一个包含所有所需文件夹路径的列表。现在我要做的就是获取这些文件夹中文本文件的所有路径,但我仍然遇到问题,我不知道如何但错误,例如
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'
采纳答案by Gaurav Vichare
You must be on windows machine. Error is because of os.listdir(). os.listdir() is not getting correct path.
您必须在 Windows 机器上。错误是因为 os.listdir()。os.listdir() 没有得到正确的路径。
And in line number 3, you are doing os.path.join("archive", directories). You should join complete path including drive (C: or D:) like "c:/archive/foo: or on linux "home/root/archive/foo"
在第 3 行,您正在执行 os.path.join("archive",目录)。您应该加入完整的路径,包括驱动器(C: 或 D:),如“c:/archive/foo:”或在 linux 上的“home/root/archive/foo”
Read - Python os.path.join on Windows
阅读 - Windows 上的 Python os.path.join
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.
在 Windows 上,当遇到绝对路径组件(例如,r'\foo')时,不会重置驱动器号。如果一个组件包含驱动器号,则所有以前的组件都将被丢弃并重置驱动器号。请注意,由于每个驱动器都有一个当前目录,因此 os.path.join("c:", "foo") 表示相对于驱动器 C: (c:foo) 上的当前目录的路径,而不是 c:\foo .
Edit:
编辑:
You are passing list corpus_path
to [os.path.join][2]
in line 6. That causes error! Replace corpus_path
with items
.
您正在将列表传递corpus_path
给[os.path.join][2]
第 6 行。这会导致错误!替换corpus_path
为items
。
I created archive folder in my 'D:' Drive. Under archive folder I created 3 folders foo1, foo2 and foo3. Each folder contains 1 or 2 text file. Then I tested your code after modification. Code work fine. Here is the code:
我在“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
output:
输出:
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']