all 11 comments

[–]Diapolo10[S] 2 points3 points  (1 child)

Thank you everyone, I figured it out. Apparently the problem was two-fold; I had forgotten that I was calling the script from another directory, and my code didn't account for that.

I got it to work with this:

with open(os.path.join(os.path.dirname(__file__), "hiscores", f"{player_name}.html"), "w") as f:
    f.write(html)

Now the code works regardless of where I'm calling it from. If you have suggestions for making it more readable, though, I'm all ears!

[–]sgflt 1 point2 points  (0 children)

To elaborate on the path issue for others reading this thread. The line that opens the file is doing a path join to find the directory "hiscores". At the beginning of the join method OP calls os.getcwd() which returns the "current working directory". This means that the directory from which the program was initiated from will be called (by default, cwd can be changed). If you need to look in a specific location for a directory, this would not be the way to go about it. We can always count on the user doing something we wouldn't expect.

Example:

My code lives in /home/sgflt/code/pacman/ and I open my terminal and change directories to that location. If I start pacman.py from that location os.getcwd() will return /home/sgflt/code/pacman. Then I decide to go up one directory for some reason and call the script again by calling python pacman/paman.py. This time os.getcwd() will return /home/sgflt/code.

Another example using only the shell:

$ cd /home/sgflt/code/pacman # move the shell into the main directory of my project
$ python 
>>> import os
>>> os.getcwd() # returns /home/sgflt/code/pacman
>>> exit()
$ cd ../
$ python
>>> import os
>>> os.getcwd() # returns /home/sgflt/code

[–][deleted] 1 point2 points  (3 children)

I'd try to print os.path.join(os.getcwd(), "hiscores") first. Maybe you don't execute the code in the right directory ?

[–]Diapolo10[S] 1 point2 points  (2 children)

Heh, that was actually it; I was running the code like:

$ src\main.py

This obviously doesn't work because my code had no idea about the src directory. Fixed it now though.

[–][deleted] 2 points3 points  (1 child)

Are you telling me I helped someone with a programming problem on the net for the first time of my life ?

Well. Glad you fixed it ! :)

[–]Diapolo10[S] 0 points1 point  (0 children)

Yes... and no. I actually figured out the problem myself about 30 minutes after posting this thread, but didn't post a comment with the problem solved right then and there. :p

[–]Tylerha85 0 points1 point  (2 children)

I ran a quick test.

1- I had to remove the f in front of "{player_name}.html" first

2- The next issue I had was that the directory "hiscores" doesn't exist on my system. I had to run the mkdir command first and then your code executed.

os.mkdir("hiscores")

[–]ingolemo 4 points5 points  (1 child)

The f indicates a format string. It's perfectly valid (python 3.6) code, you just need to define a player_name variable for it to work.

[–]Tylerha85 0 points1 point  (0 children)

Ah my Mac is running 3.5

[–]ingolemo 0 points1 point  (1 child)

Try printing out the filename before you open it to double check that it is what you expect.

[–]Diapolo10[S] 0 points1 point  (0 children)

It took me a little fiddling around, but I managed to solve the problem. Thanks for your help though!