0

To whomever it concerns, the linked answers do not work! Dear admin, what changes do I need to do to get this back open?

Can not get python to open a file in a directory no matter what I try. It is a json file in known location.


I'm on windows 10, I am making my first python file using Visual Studio Code.

I've tried all iterations I can think of: \\, //, /, with and without r. It just won't accept the path or does something weird to it.

I know this is super simple issue and there are tons of solutions for it but they just don't work.

I have tried the 3 solutions from the supposed linked solution, none work:

Error messages with different syntax:

1:

with open(r"C:\Path\Sam\about\cs\ffiles\up\u_file.json") as open_file:
    data = open_file.read()
    file_data = json.loads(data)
    print(file_data)

I get:

Traceback (most recent call last):
  File "d:\...\test1.py", line 45, in <module>
    with open(r"C:\Path\Sam\about\cs\ffiles\up\u_file.json") as open_file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Path\\Sam\\about\\cs\\ffiles\\up\\u_file.json'

Another try, different syntax:

with open("C:\\Path\\Sam\\about\\cs\\ffiles\\up\\u_file.json") as open_file:
    data = open_file.read()
    file_data = json.loads(data)
    print(file_data)

I get:

  File "d:\...\test1.py", line 45
    with open("C:\\Path\\Sam\\about\\cs\\ffiles\\up\\u_file.json") as open_file:
                                                                                                                 ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 85-86: truncated \uXXXX escape
PS D:\...>

Third example of the supposed solution, does not work either:

with open("C:\Path\Sam\about\cs\ffiles\up\u_file.json") as open_file:
    data = open_file.read()
    file_data = json.loads(data)
    print(file_data)

I get:

Traceback (most recent call last):
  File "d: ...test1.py", line 45, in <module>
    with open("C:\Path\Sam\about\cs\ffiles\up\u_file.json") as open_file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Path\Sam\about\cs\ffiles\up\u_file.json'
PS D:\...>

The file path is 100% correct. I can copy the path, press win-r, paste the path and windows opens the file.

vinter5
  • 37
  • 5
  • 2
    Using `r'C:\...'` should be the way to solve the `UnicodeError`. The `FileNotFound` error doesn't seem very weird to me, it just means that the file doesn't exist at that location. So please check if you spelled the path correctly and if the file exists there (my guess would be that `Path)` should be `Path`) – Clasherkasten Jun 01 '23 at 15:16
  • the file path is directly copied from windows file manager. I am thinking this is some kind of weird visual code issue..? – vinter5 Jun 01 '23 at 15:23
  • If you think this is an VSCode issue, did you try to run it outside of VSCode? If that error still persists when doing so, this is something with the file/path (which i guess), – Clasherkasten Jun 01 '23 at 15:29
  • Probably a cut and paste error but you might want to validate `Path)` that you are sometimes using. – JonSG Jun 01 '23 at 15:31
  • pycharm gives the same errors. I'd imagine downloading more coding apps does not solve the issue. – vinter5 Jun 01 '23 at 15:34
  • You must supply the full error traceback in each case where it `doesn't work`. – quamrana Jun 01 '23 at 15:36
  • The last edit just shows what happens when you think you are at a `DOS` prompt, but really you are still in python. – quamrana Jun 01 '23 at 15:46
  • what do you mean? – vinter5 Jun 01 '23 at 15:50
  • What is the point of the last edit? How does it improve your question that is asking about a different syntax error or a file not found error? – quamrana Jun 01 '23 at 15:51
  • I am just trying all kinds of different ways to make it work and posting the different errors I get. I am just trying to provide more information. – vinter5 Jun 01 '23 at 15:53
  • 1
    `C:\Path)` Why is that parentheses there? – John Gordon Jun 01 '23 at 15:53
  • 1
    You need to look at the duplicate link first. Your first error is about string literals in python. Your strings have: `\u` in them which looks like you are wanting to include a unicode character. Using `r'...'` completely sidesteps that issue. Then it is a simple file not found error. Go look for the file. – quamrana Jun 01 '23 at 15:56
  • I have tried multiple json files in different directories, copied the same file and tried different file. It just refuses to read the path correctly and I have no idea why. – vinter5 Jun 01 '23 at 15:58
  • Your last code snippet works for me: Without the file present, I get the file not found. When I create the required file within that folder structure the code works and prints the `json` contents. – quamrana Jun 01 '23 at 16:01
  • Does your path literally have the directory `Path` in it, or are you using that to redact some personal information? If so, I suspect the problem is in the redacted part that we can't see. – slothrop Jun 01 '23 at 16:03
  • _and I have no idea why_ If it says the file isn't there, then **that's why**. – John Gordon Jun 01 '23 at 16:03
  • I tried the 3 different things. None of them work. When I take the path to the json file and press win-r and paste it there it opens the file it is supposed to. The path is right! – vinter5 Jun 01 '23 at 16:06
  • _`can't decode bytes in position 85-86:`_ That error message makes no sense, because the filename string is only about 45 characters long. Are you SURE this is your real code? – John Gordon Jun 01 '23 at 17:16
  • `FileNotFoundError: [Errno 2] No such file or directory: 'C:\Path\Sam\about\cs\ffiles\up\u_file.json'` That error looks rock-solid to me. **That filename does not exist.** – John Gordon Jun 01 '23 at 17:22
  • If you `cd` to that directory in a command window, and type `dir`, does it show the file? – John Gordon Jun 01 '23 at 17:23
  • You mean in cmd? Yes, it shows up. I still think the folder path just gets mangled somehow or that visual code can't just access the file... – vinter5 Jun 01 '23 at 17:54
  • I just wanted to add that I solved the issue. It was something wrong with vscode. I think with workspace. Closing all files and restarting vscode and then opening the files again made it work. I think I may have changed something at some point that made vscode not able to open any paths. – vinter5 Jun 30 '23 at 11:12

0 Answers0