1

I tried working on files in python and learned that vsc doesn't execute the right folder. It executes parental folder instead of sub folder.

highscore.txt has to be in parental folder workspace1 in order for it to work, i want it to be in sub folder snake-game, but then it's not working

XX

This is the error I get:

FileNotFoundError: [Errno 2] No such file or directory: 'highscore.txt'

Edit: I'm using code runner in order to run code

I changed this setting but it didn't do anything

it is enabled

I've got absolutely no idea how to work on directories, could someone tell me what to change and where to fix it.

7
  • Can you be more specific: what file is not found? How are you referencing it? What are you trying to execute and how are you executing it?
    – topsail
    Jun 4 at 14:42
  • this is how i open code: ''' with open("highscore.txt") as data: final = data.read() self.high_score = int(final) '''
    – slydragon
    Jun 4 at 14:45
  • since highscore.txt is in a directory above, you should be able to just use a relative path: with open("../highscore.txt" as data:
    – topsail
    Jun 4 at 14:48
  • @topsail the thing is that the code works, when highscore.txt is outside of snake-game folder. I want it to work with highscore.txt being inside of snake-game folder. i'm using code runner to run code btw here is the code runner option to run python code: "python": "python -u",
    – slydragon
    Jun 4 at 14:52
  • In your screenshot, highscore.txt is not in the snake-game folder. So maybe you just need to put it there??? Otherwise, please confirm where where the file really is, and where is the code that you are using to open the file, and what that code is!
    – topsail
    Jun 4 at 15:14

2 Answers 2

1

I don't think running a code through a code runner may be problematic, but you should verify to make sure you wrote the right path and that your file is in the game folder.

0
0

When you check Execute in File Dir, you have to use Run Python File button which provided by Python extension.

enter image description here

You can also add "cwd": "${fileDirname}" to your launch.json and use debug mode.

If you insist on using the coder-runner extension, please search and check File Directory As Cwd in the settings.

enter image description here

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.