1

When I am using Absolute path the code is working fine but using relative path throwing FileNotFoundError in python.

 f = open("Input.txt","r")

enter image description here

Hmm
  • 23
  • 1
  • 7
  • 1
    Does this answer your question? [Why am I getting a FileNotFoundError?](https://stackoverflow.com/questions/17658856/why-am-i-getting-a-filenotfounderror) – Mahrkeenerh Oct 29 '21 at 12:50
  • The issue is similar but it can't resolve my error. One thing more if I am creating file using - Test_File = open("testFile.txt","w") - The file isn't created inside the current directory , it created inside the parent directory. – Hmm Oct 29 '21 at 12:59
  • so it answers your issue perfectly - you need to include your folder in the path: `"Python_automation/test.txt"` – Mahrkeenerh Oct 29 '21 at 13:01
  • Yes it is working by this way and by including the full path but i am curious about why its not working with relative path ( The guy in the tutorial using the same technique and its working for him) – Hmm Oct 29 '21 at 13:09
  • `"Python_automation/test.txt"` already is relative path - relative to the project. If you launch your script via cmd, you have to only use `"test.txt"` – Mahrkeenerh Oct 29 '21 at 13:10

2 Answers2

6

Your python file is executed by the terminal. You can clearly see that your terminal is at the folder ...Desktop\cs\Python\myproject\. Since the file "Input.txt" does not exist relative to the path of your terminal, you are getting this error. (That is, the path ...Desktop\cs\Python\myproject\Input.txt does not exist)

A simple solution would be to use absolute path in your python file instead of the relative path.

Another cheap solution is to use the terminal, go to the correct folder and run your file, as intended by God.

If you really want to dedicate a single button for running, you can try the following:

EDIT: Okay, I understand you are using the "Run button" at top of python files to run.

You only need to set the setting python.terminal.executeInFileDir to true.

In Settings, search for python.terminal.executeInFileDir and mark it. That should be what you need. enter image description here

AzuxirenLeadGuy
  • 2,470
  • 1
  • 16
  • 27
  • Hey, Now I have understood the path issue but even after configuring the launch.json it is still not working. – Hmm Oct 29 '21 at 13:27
  • @TJSM are you running the file by pressing F5? launch.json only applies to that – AzuxirenLeadGuy Oct 29 '21 at 13:28
  • oh thanks its working but its running debugger too . One thing more where should I create the file vsCodeOpenFolder.reg. I have updated to window 11 and the "open with vscode" isn't appearing. – Hmm Oct 29 '21 at 13:33
  • @TJSM In regards to "open with vscode" option, I believe it is easier to re-install. I found this guide so you can check it out as well: https://thisdavej.com/right-click-on-windows-folder-and-open-with-visual-studio-code/ – AzuxirenLeadGuy Oct 29 '21 at 13:45
  • @TJSM Please mark this answer as accepted if this worked for you – AzuxirenLeadGuy Oct 30 '21 at 04:42
  • This setting will not work if you open a folder, even after restarting the application. I even uninstalled VS Code and installed it again to see if it was a corruption issue, it kept giving me the File not in directory or directory not found error. But I closed the folder and opened the file by itself, and the problem was no more. If you want to work as a normal developer opening the project folder, you need to use full paths, which is very inconvenient. I'm new to Python so I don't know the ramifications of this. FYI – raphie Sep 08 '22 at 03:34
0

A quick solution to use relative paths can be to right click on the file, copy relative path and replace "" with "/". You can do it manually or with the function .replace("","/").

"route\input.txt".replace("","/")