[Tutor] Ideas about global variables for accessing project's file

Walter Prins wprins at gmail.com
Wed Jan 12 14:36:44 CET 2011


Hi,

On 12 January 2011 13:11, Kann Vearasilp <vearasilp at gmail.com> wrote:

> Below is the structure of my project
>
> /src
> /src/modules/mod1
> /src/modules/mod2
> /src/data/file1
> /src/data/file2
>
>
> I wrote some codes in mod1 to access data in file1. And to read the file1,
> I am hard coding in the mod1, which is something like...
> open(blahblah/src/data/file1, "w")
> Now the problems arise when I share my code with other collaborators and
> the code is not working any more because his/her blahblah before /src is
> different.
>
> Is there a way to make a global variable for the project to make my life
> easier? Sometime like... PROJECT_PATH/src/modules/mod1 which PROJECT_PATH
> should work with every machines.
>

1.) You should be able to use relative paths, e.g. something like:
open("../data/file1", "w")
2.) You can set up configuration for your application to define where the
data files live.  See e.g. the "ConfigParser" module:
http://wiki.python.org/moin/ConfigParser

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110112/c4f7f5ac/attachment.html>


More information about the Tutor mailing list