[Tutor] Opening files, finding their location

Scott Oertel me at scottoertel.info
Sat Oct 8 20:00:04 CEST 2005


Christopher Arndt wrote:

>Scott Oertel schrieb:
>  
>
>>I'm looking for an easy way to find the current directory location of my 
>>program so I can include the config.ini file correctly without having to 
>>pass command line args.
>>    
>>
>
>So, do you want to find out, where your script is living, or the directory from
>which it was called (the "working directory")?
>
>The former can be found out like this:
>
>Assuming cron calls your script with the full path name (which should be the
>case because it uses the shell to execute your script):
>
>  
>
>>>>import os, sys
>>>>mydir = os.path.dirname(sys.argv[0])
>>>>        
>>>>
>
>When you want the current working directory:
>
>  
>
>>>>import os
>>>>mydir = os.getcwd()
>>>>        
>>>>
>
>Then to get the path to your config file, either:
>
>config_path = os.path.join(mydir, 'config.ini')
>
>HTH, Chris
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>  
>
It was this one:

mydir = os.path.dirname(sys.argv[0])

thanks man, it was starting to drive me nuts :)

-Scott Oertel
-Gawr.com!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20051008/42b52908/attachment.htm


More information about the Tutor mailing list