加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:[错误3]系统找不到指定的路径:

发布时间:2020-12-20 13:52:45 所属栏目:Python 来源:网络整理
导读:import osCurrent_Directory = os.getcwd() # Should be ...archiveCORPUS_PATHS = sorted([os.path.join("archive",directories) for directories in os.listdir(Current_Directory)])filenames = []for items in CORPUS_PATHS: filenames.append(sorted([
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”,目录).
你应该加入完整的路径,包括驱动器(C:或D :),如“c:/ archive / foo:或在linux上
“家用/根/存档/富”

阅读 – Python os.path.join on Windows

os.path.join Usage –

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:archivefoo1
['d:archivefoo1foo1.txt.txt','d:archivefoo1foo11.txt']
d:archivefoo2
['d:archivefoo2foo2.txt.txt']
d:archivefoo3
['d:archivefoo3foo3.txt.txt']

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读