0

Debugging turned out not to be a problem. The file was running in a folder above its own folder so using a relative file location fixed the problem. Still, the fact that it would not run in its own folder is perplexing.

When run without debugging, it works perfectly fine but when run with debugging, there is an error message: Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: 'piratein.txt'

input = open("piratein.txt")

distances = (input.readlines())
l = int(distances[0])
x = int(distances[1])
y = int(distances[2])

if x+y<l:
    write = str(x+y)

elif x+y>l:
    write = str((l-x)+(l-y))

#creates and opens an output file for writing
output = open("pirateout.txt", "w")
output.write(write)

#the input file is only 3 lines, each with a single integer
#the path of the input file is "\CODING\VisualStudioCode\Informatics Olympiad\AIO_PRACTICE\piratein.txt"
#and the python file is in the same folder as the input file
Aaron
  • 3
  • 4
  • because when you run the vscode debugger it uses the files directory as the current working directory. And when you don't it uses your terminals current working directory. – Alexander Aug 29 '22 at 02:26
  • https://stackoverflow.com/a/72757920/2823755 – wwii Aug 29 '22 at 02:32
  • Check if the file "pirateout.txt" exist on wherever you place your release exe – Brian MJ Aug 29 '22 at 02:58
  • @BrianMJ I am not sure what you mean by release exe. If you meant, where the python file is, when running without debugging, the output text file is created in the same directory as the python file. – Aaron Aug 29 '22 at 05:15
  • In your own words: when you tell Python to open a file named `piratein.txt`, *how do you think Python decides where to look* for the file? What folder should the file be in, and why? – Karl Knechtel Aug 30 '22 at 04:28

2 Answers2

0

double-check that the directory that you are running the python file from,(as seen in the terminal), is also where pirateout.txt is stored. You can use a combination of 'ls' and 'cd ' in the command line to navigate to where it is

rohan
  • 16
  • 3
  • Oh yeah, I forgot to add this in the original post but I tried this already and navigating to the same folder as the files in terminal did not change the error message. – Aaron Aug 29 '22 at 04:33
0

In launch.json, there are settings about cwd, which specifies the search range of files during debugging. Please put the file piratein.txt in this folder.

enter image description here

Example1:

workspace
-src
--test
---test.py
-helloworld.txt

enter image description here

Example2:

workspace
-src
--test
---test.py
---helloworld.txt

enter image description here

MingJie-MSFT
  • 5,569
  • 1
  • 2
  • 13
  • Sorry, I am a little confused by your answer. I created a launch.json file and attempted to edit it, partially using https://stackoverflow.com/a/55072246/19863358 as a guide. I added in: "cwd": "${workspaceFolder}" so that the program uses the working directory of the program which is also where the input file is stored but nothing has worked :( – Aaron Aug 29 '22 at 05:06
  • Then you need to place the txt file in the same directory as the python file。 – MingJie-MSFT Aug 29 '22 at 05:08
  • They already are in the same directory though. – Aaron Aug 29 '22 at 05:11
  • @Aaron I added my answer. Is it feasible for you to debug in this way – MingJie-MSFT Aug 29 '22 at 05:15
  • Yes, I can click the settings icon and open the launch.json file but then what should I add to change the search range of files during debugging? – Aaron Aug 29 '22 at 05:23
  • @Aaron I think this seems to be a problem of directory structure. By default, vscode identification files are retrieved with the current workspace as the root directory. I think your python file may be saved in a deeper directory. At this time, txt file should be placed in workspace. I'll give you an example in minutes. – MingJie-MSFT Aug 29 '22 at 05:32
  • @Aaron Does my solution solve your problem? – MingJie-MSFT Aug 30 '22 at 02:01
  • Yes, it did. I never thought to try to use a relative file path. This means that even though the python file is in a subfolder, it is still running from a folder above which perplexes me. Still, using the relative file path works. Thanks for all your help! @MingJie-MSFT – Aaron Aug 30 '22 at 04:14