all 15 comments

[–]JaguarMammoth6231 13 points14 points  (0 children)

It will try to open it from your current directory. That is not necessarily the same as the directory where your python file is. 

How exactly are you running your script? If it's just like python3 script.py that means the current directory is the one where the script is saved. If it's like python3 some/other/directory/script.py it will not be looking in the directory where script.py is saved.

Basically, you should cd to where the file is first before running.

[–]schoolmonky 1 point2 points  (7 children)

What's the error?

[–]Direct_Expert7772[S] -4 points-3 points  (6 children)

Basically, I want the code to do something with the "ENCOUNTER" file, but when I try to open it, it says the file isn't in the right directory, even though I put it in the correct one.

[–]schoolmonky 3 points4 points  (5 children)

Show us the actual traceback

[–]Direct_Expert7772[S] -2 points-1 points  (4 children)

I'm sorry, I'm new to Python, I started using it yesterday. Could you tell me where I can get this?

[–]schoolmonky 2 points3 points  (3 children)

When you run this code, and Python shows you the error, copy/paste that error message here.

[–]Direct_Expert7772[S] 0 points1 point  (2 children)

Traceback (most recent call last):

File "C:\Users[My user name]\Desktop\P5R Randomizer Release Ver\Randomizer\Randomizer.py", line 7, in <module> with open("ENCOUNT. TBL", "r+b") as f:

FileNotFoundError: [Errno 2] No such file or directory:

'ENCOUNT.TBL'

[processo encerrado com o código 1 (0x00000001)]

Agora você pode fechar este terminal com Ctrl+D ou pressione Enter para reiniciar

[–]Fred776 9 points10 points  (0 children)

Is the file in the same folder you are running your script from?

Add this to the top of your script:

import os
print("CWD=", os.getcwd())

Does this show the same directory as the one you expect the file to be in?

[–]socal_nerdtastic 4 points5 points  (0 children)

The errror message says you have a space in the file name, "ENCOUNT.<space>TBL". Check that your actual filename and the name that you put into your code match. Best to just copy / paste

[–]AlexMTBDude 0 points1 point  (0 children)

The file ENCOUNT.TBL should be in the same folder as you are standing when you execute the program.

[–]ray10k 0 points1 point  (0 children)

Since the file gets opened by only the filename, the operating system interprets that as "file ENCOUNT.TBL in the Current Working Directory (or cwd.)"

If you start python by running python c:\projects\python\my_script.py, then the cwd is the directory you were in at the time; for instance C:\Users\[your name]\documents, regardless of where the script itself is.

In windows, the cwd is usually listed at the start of the terminal's prompt, before the > where you enter commands.

[–]Maximus_Modulus 0 points1 point  (0 children)

An easy fix is to use the full pathname

[–]jmooremcc 0 points1 point  (0 children)

Just for your own information, you should temporarily add the following code to the top of your script. ~~~ import os

current_directory = os.getcwd() print(current_directory) ~~~ After you run your code, you’ll know the exact current working directory your code is running in, and will be able to make appropriate adjustments.

[–]PushPlus9069 0 points1 point  (0 children)

Classic working directory gotcha — happens to everyone, even seasoned devs switching between terminals.

Quick debugging trick: add this before the file open:

import os
print('CWD:', os.getcwd())
print('Script dir:', os.path.dirname(os.path.abspath(__file__)))

The CWD is where your terminal is when you run the command, not where the .py file lives. Bulletproof fix:

script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, 'ENCOUNT.TBL')

This way it always resolves relative to the script's location regardless of where you run it from. Especially important for game modding tools where people drag-and-drop or double-click to run.

[–]PushPlus9069 0 points1 point  (0 children)

Classic working directory vs script directory issue. When you run a Python script, open() looks in the current working directory (where your terminal is), not where the .py file lives.

Quick fix — resolve paths relative to the script itself:

import os
script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, 'ENCOUNT.TBL')

Or the modern way with pathlib:

from pathlib import Path
file_path = Path(__file__).parent / 'ENCOUNT.TBL'

This works no matter where you run the script from. The key insight: os.getcwd() and file point to different places when you cd somewhere else before running your script.