简体   繁体   English

无法在python上读取文件

[英]Unable to read file on python

I'm trying to read a file using below code,我正在尝试使用以下代码读取文件,

f = open("test.txt", "r")
print(f.read()) 

test.txt is saved under the same folder as the py file under below path, test.txt保存在与下面路径下的py文件相同的文件夹下,

C:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\

with above code I get below error message,使用上面的代码,我收到以下错误消息,

(env2) PS C:\Users\Benjamin Chen\Desktop\Python Codes> cd 'c:\Users\Benjamin Chen\Desktop\Python Codes'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\Scripts\python.exe' 'c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py' '--default' '--client' 
'--host' 'localhost' '--port' '58754' 'c:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\freecode17.py' 
Traceback (most recent call last):
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Python\Python38\lib\runpy.py", line 263, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Python\Python38\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\freecode17.py", line 1, in <module>
    f = open("test.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

I did some search online and found that I may need to change the file path to it's absolute path, so I tried below code,我在网上做了一些搜索,发现我可能需要将文件路径更改为绝对路径,所以我尝试了下面的代码,

f = open("C:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\test.txt", "r")
print(f.read()) 

And received below error,并收到以下错误,

(env2) PS C:\Users\Benjamin Chen\Desktop\Python Codes> cd 'c:\Users\Benjamin Chen\Desktop\Python Codes'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'C:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\Scripts\python.exe' 'c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py' '--default' '--client' 
'--host' 'localhost' '--port' '58913' 'c:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\freecode17.py' 
Traceback (most recent call last):
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\Benjamin Chen\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Python\Python38\lib\runpy.py", line 262, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\Python\Python38\lib\runpy.py", line 237, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "c:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\freecode17.py", line 1
    f = open("C:\Users\Benjamin Chen\Desktop\Python Codes\python_env\env2\test.txt", "r")
             ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

\\ is an escape character. \\是转义字符。 If you want a \\ literal in your string (path), you'll have to escape it with another \\ :如果您想在字符串(路径)中使用\\文字,则必须使用另一个\\对其进行转义:

f = open("C:\\Users\\Benjamin Chen\\Desktop\\Python Codes\\python_env\\env2\\test.txt", "r")
# Here -----^------^--------------^--------^-------------^-----------^-----^

The backslash is used to escape special characters to instruct the interpreter to interpret the character sequence immediately following it in a specific way.反斜杠用于转义特殊字符,以指示解释器以特定方式解释紧随其后的字符序列。 It is a kind of control character.它是一种控制字符。 To include a literal backslash within a string, you must escape it.要在字符串中包含文字反斜杠,您必须对其进行转义。 Replace all of the backslashes in your path with double backslashes and you should have a fix.用双反斜杠替换路径中的所有反斜杠,你应该有一个修复。 See the Wikipedia entry Escape Characters for more information.有关详细信息,请参阅 Wikipedia 条目转义字符

Additionally, the error message tells you precisely where you went wrong.此外,错误消息会准确地告诉您哪里出错了。 It states that characters 2-3 are funny.它指出字符 2-3 很有趣。 Character 2 in the path string is the backslash.路径字符串中的字符 2 是反斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM