-1

I am having a problem open a simple text file in Python 3.8. I setup a simple test.

Here is my test code:

import os

file_path = "c:\Users\username\Documents\folder1\some_file.txt"

with open(file_path, 'r') as f:
    for line in f:
        print(line)

I get the following error: Unicode Error "unicodeescape" codec can't decode bytes in position 2-3.

I have read other posts about putting an 'r' in front of the file path and when I do I get an "No such file or directory: 'c:\Users\username\Documents\folder1\some_file.txt'

import os

file_path = r"c:\Users\username\Documents\folder1\some_file.txt"

with open(file_path, 'r') as f:
    for line in f:
        print(line)

I have also tried using double backslash in the path c:\\Users\\username\\Documents\\folder1\\some_file.txt and that did not work either.

I have tried a test using pathlib and still get unicode error.

from pathlib import Path

file_path = "c:\Users\username\Documents\folder1\some_file.txt"

file_path = Path(file_path).absolute()

with open(fpath, 'r', encoding='utf-8') as f:
    line = f.readlines()
    for line in f:
        print(line)
D Chase
  • 85
  • 7
  • What was the error with double backslashes? In a normal Python string \u is the start of a unicode character, which Python can't decode since \Users and \usern are not valid unicode characters. Are you certain your file path is correct? – Altareos May 21 '21 at 19:27
  • @Altareos. The error is the same "No such file or directory". I have confirmed the file path is correct. – D Chase May 21 '21 at 19:30
  • 2
    I'm willing to believe the error message that the file simply couldn't be found. For testing, move the file to a simpler location (like the root of the C: drive) and try it with that path. – Antimon May 21 '21 at 19:34
  • 1
    Do an `os.listdir(r"c:\Users\username\Documents\folder1")` to see exactly what Python is seeing in that directory - perhaps the file is actually named `some_file.txt.txt`, and Windows is helpfully hiding one of the extensions. – jasonharper May 21 '21 at 19:34
  • Just to make sure... you're not literally trying to look in a folder named `username`, right? – Antimon May 21 '21 at 19:39
  • @jasonharper Very good suggestion. (I keep forgetting myself that Windows does that because it's one of the first things I deactivate on any installation I work with.) – Antimon May 21 '21 at 19:59
  • @jasonharper I did `os.listdir(r"c:\Users\username\Documents\folder1")` and it listed `some_file.txt`. @Antimon, no, i am not trying to look in a folder named "username" i stripped that out. I moved the document to c:\Users\username\Documents and received the same error. – D Chase May 21 '21 at 20:14
  • @Altareos I noticed my file name is actual `some file.txt` (no underscore). Do you think that has anything to do with it? – D Chase May 21 '21 at 20:33
  • An underscore and a space are completely different characters, of course the file names are not the same. – Mark Ransom May 22 '21 at 03:49

2 Answers2

1

In your first example, file_path = "c:\Users\username\Documents\folder1\some_file.txt" The \U in \Users represents a Unicode escape sequence and it is trying to decode sers as a Unicode character, which it is not.

On my machine, the double backslash seems to work - but of course I do not have a text file at that path so I can not really test.

Try first the double backslash for just the \U.

  • I tried the double backslash for just the \U `c:\\Users\username\Documents\folder1\some_file.txt` and get the "No such file or directory" error – D Chase May 21 '21 at 19:39
0

Further information:

As Andrew Kaluzniacki's answer rightly pointed out, the trouble was with the path string.

Using pathlib.Path

To prevent hassle with Windows paths and delimiters, one could opt to always use forward slashes, as both open() and Windows have mechanisms of handling this.

However, for the sake of backward compatibility and robustness it is arguably better to use pathlib.Path from the standard library (still using forward slashes as path separators). pathlib automatically converts the filepath to one suited to your OS.

Lastly, os.path.filesep() returns the file separator for the host OS.

Another potential cause for people having a similar issue

There may be a character in the file not covered by your operating system's default encoding.

Try other encodings by passing a different encoding to the encoding parameter of open().

You could try open(file_path, 'r', encoding='utf-8'), although that should be the default for Windows 10, assuming that is your OS based on you filepath examples.

Without knowing what is in the file, it is hard to know which encoding would work, however.

from pathlib import Path

fpath = Path(fpath).absolute()
# ^^ absolute() is not necessary if
# the file is in the same directory
# as the calling Python script
# and you just pass a filename.

with open(fpath, 'r', encoding='utf-8') as filehandle:
    do_something_with(filehandle)

jrbergen
  • 660
  • 5
  • 16
  • 2
    The encoding problem comes from the path string. Try putting `"c:\Users\username\Documents\folder1\some_file.txt"` in your interpreter, you'll see the same error. – Altareos May 21 '21 at 19:31
  • 1
    i copied `c:\Users\username\Documents\folder1\some)fille.txt"` and get the same unicode error. I copied `c:\\Users\username\Documents\folder1\some)fille.txt"` and it was echo'd back with no error – D Chase May 21 '21 at 19:35
  • 1
    To eliminate the hassle with Windows path delimiters, you can also just use forward slashes. Even Windows itself recognizes those, including the Explorer address bar, cmd interpreter, and PowerShell. – Antimon May 21 '21 at 19:49
  • @Altareos Ah I overlooked that. I'll adjust the answer. – jrbergen May 21 '21 at 19:53
  • @Antimon that's new to me. Good to know. Will update my answer (or should I remove it? Im a relatively new SO user). – jrbergen May 21 '21 at 19:54
  • 1
    Good question. Maybe keep what you've written about `pathlib.Path`, but add the information about forward slashes as another alternative and group both under a heading like "Further info" or so? – Antimon May 21 '21 at 19:57