My Project Euler setup is a PyDev project that I work on from two different computers (one is a Mac, the other is Windows 7). The file structure of the project looks like this:
PROJECT_LOC/
unsolved/
The .py files for the problems I haven't solved
solved/
Problems_001_025/
.py files for problems 1-25
... etc ...
texts/
Any input files provided by Project Euler (e.g., Problem 22)
Every file is named in the pattern Problem###.py
or Problem###.txt
.
Once I solve a problem, I move it from unsolved
to the correct directory in solved
, which is where my difficulty comes in:
Given a problem with input, say Problem022.py
:
for line in open("../texts/Problem022.txt"):
# read file in
# code to solve the problem
Since I solved Problem 22 a while back, I moved it from PROJECT_LOC/unsolved/
to PROJECT_LOC/solved/Problems_001_025/
. Now, (not surprisingly) when I try to run it again, it gives me a no such file error.
So, without changing my file structure, is there a way for me to access the input text files from wherever I am in the project directory?
I was thinking I could do something like open(${PROJECT_LOC}/texts)
, but I have no idea how to get the PROJECT_LOC
from Eclipse at run time, and have it work on both Windows and OS X. I played with what this person did in his question, but couldn't get it to work for me.