FileNotFoundError:[错误2]-可能是工作网络问题?

时间:2019-09-09 22:47:48

标签: python pycharm

我正在尝试从工作网络读取工作网络上文件的内容。我复制并粘贴了来自Google搜索的代码段,并将其修改为以下内容。为什么我仍然会收到[Errno 2](我已经更改了此问题面板的某些路径名)

我的文件浏览器中的文件路径显示“>“此PC>普通字”,而我的路径中没有“此PC”。我尝试将其添加到我认为会出现在字符串中的位置。那没有解决。

我尝试确保我的字母大小写匹配。那没有解决。

我尝试重命名该文件以使末尾带有* .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/FailureCompiling/FirstScriptAttempt.py   追溯(最近一次通话):

     

文件“ C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.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”)#打开lorem.txt以读取文本

     

FileNotFoundError:[错误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:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.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'

我要添加下面的图片的目的是显示文件资源管理器如何显示我的文件的文件路径 FileExplorerPathScreenShot

编辑 我认为这可以确认我的“操作系统”没有文件。

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

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

编辑

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

编辑-成功

我跑了

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

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

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

谢谢@Furas

1 个答案:

答案 0 :(得分:0)

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

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替换为您的文件名。