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)