Fat Wallets
Fat Wallets

Reputation: 101

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,

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

Upvotes: 1

Views: 253

Answers (2)

pkfm
pkfm

Reputation: 461

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.

Additionally, the error message tells you precisely where you went wrong. It states that characters 2-3 are funny. Character 2 in the path string is the backslash.

Upvotes: 1

Mureinik
Mureinik

Reputation: 312106

\ 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 -----^------^--------------^--------^-------------^-----------^-----^

Upvotes: 1

Related Questions