n
18

Spent two days trying to get my Python script to read a simple text file

I kept getting a 'file not found' error even though I was sure the path was right. Turns out I was running the script from a different folder than I thought, so the path was wrong. How do you guys keep track of where your files are when you're just starting out?
3 comments

Log in to join the discussion

Log In
3 Comments
avery897
avery8971mo ago
Always use absolute paths when you're testing, it saves so much headache. I make a habit of printing out the current working directory at the start of my scripts with os.getcwd(). That way you can see exactly where Python thinks it is.
7
noranguyen
noranguyen17d ago
Oh absolutely, I learned that the hard way too. I spent half a day once trying to figure out why my script couldn't find a config file, and it was just because I was running it from a different folder than I thought. Printing the cwd right away is such a simple fix.
5
noah_webb
noah_webb1mo ago
Maybe try a relative path with pathlib, like Path(__file__).parent, since @avery897 already covered checking the cwd.
0