文件未找到错误: [错误 2] - 可能的工作网络问题?



>我正在尝试从工作网络读取工作网络上的文件内容。 我从谷歌搜索中复制并粘贴了一个代码片段,并将其修改为以下内容。 为什么我仍然会得到[Errno 2](我已经更改了这个问题板的一些路径名(

我的文件资源管理器中的文件路径显示">这台电脑>"通用词",而我的路径中没有"这台电脑"。 我尝试将其添加到我认为它在字符串中的位置。 这并没有解决它。

我尝试确保我有匹配的大写。 这并没有解决它。

我尝试重命名文件以在末尾显示 *.txt。 这并没有解决它。

我尝试了//和/和 \ 的不同变体,有和没有 r 前身,虽然这确实消除了我得到的第一个错误。 它对此错误没有帮助。

(查看右装订线中的代码错误表示我的行长大于 PEP8 标准。 虽然我怀疑这是我问题的根源,但如果你能为这么长的文件路径提供"正确"的包装方法,那将是有帮助的。

myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt")  # open lorem.txt for reading text
contents = myfile.read()         # read the entire file into a string
myfile.close()                   # close the file
print(contents)                  # print contents

完整错误复制:

C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureComcompling/FirstScriptTry.py 回溯(最近一次调用(:

文件 "C:/Users/e087680/PycharmProjects/FailureComcompling/FirstScriptTry.py",第 1 行,在 myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt"( # open lorem.txt 用于阅读文本

FileNotFoundError: [Errno 2] 没有这样的文件或目录: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'

编辑

调试工作以弄清楚如何更改目录。 以防万一这就是问题所在。 已测试此代码位

import os
path = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)

收到此错误

C:Userse087680PycharmProjectsFailureCompilingvenvScriptspython.exe C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py", line 5, in <module>
os.chdir(path)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'

我添加下图的目的是显示文件资源管理器如何显示我的文件的文件路径 文件资源管理器路径屏幕截图

编辑我认为这证实了我的"操作系统"没有我的文件。

from os import path
path.exists("PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246")
def main():
print ("File exists:" + str(path.exists('PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246')))
if __name__== "__main__":
main()

输出

File exists: False

我认为操作系统是操作系统的标准变量。 现在我不确定。

编辑

在DOS中使用Cmd,我确认我的z:路径是正确的

编辑- 成功

我跑了

import os
print( os.listdir("z:/"))

确认我不需要文件夹的怪物字符串。 已确认,虽然资源管理器没有显示它,但它是一个 *.txt 文件

一旦我实现了这两个项目,第一个代码就可以正常工作。

谢谢@Furas

要打开和读取文件,请在路径中指定文件名:

myfile = open("U:/matrix_neo/word common common/hello world.txt", "rt")  # open file
contents = myfile.read()         # read the entire file into a string
myfile.close()                   # close the file
print(contents)                  # print contents

U: 是我的网络中的映射驱动器。 我没有发现您的更改目录示例有任何问题。我再次在我的 U: 路径上使用了一个路径,它返回了 True。

import os
path = "U:/matrix_neo/word common common"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)

检查您尝试从中读取的目录上的属性。此外,请尝试将文件复制到本地驱动器进行测试,看看是否可以读取该文件并检查它是否存在。

这是上述方法的替代方法,并使用您的路径来确保长文件路径正常工作:

import os
mypath = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
myfile = 'whatever is your filename.txt'
if not os.path.isdir(mypath):
os.makedirs (mypath)
file_path = os.path.join(mypath, myfile)
print(file_path)
if os.path.exists(file_path) is True:
with open(file_path) as filein:
contents = filein.read()
print(contents)

我用一个长csv文件测试了这段代码,将变量myfile替换为你的文件名

相关内容

最新更新