[Tutor] How to change default path output of 'logging'?

Dave Angel davea at davea.name
Mon Dec 22 17:57:00 CET 2014


On 12/22/2014 11:41 AM, Juan Christian wrote:

>
> str(os.path.abspath(__file__)).replace('main.py', '')
>
> So I get the full-path of my main dir where main.py is and all the other
> modules/packages too, no matter if I'm on OSX/Win/Linux.

That's not the best way to get the directory path for a file.  You 
should use  os.path.dirname().  That way if the basename changes from 
main.py to something else (like main.pyc, or main.pyw, or 
usefulcode.py), it'll still work.  It fixes other problems also, that 
are less likely to occur.

os.path.dirname(os.path.abspath(__file__))  should do it for you.  And 
that means that you want something like:

filename=os.path.join(os.path.dirname(os.path.abspath(__file__)), 
"bumpr.log")

(untested)


-- 
DaveA


More information about the Tutor mailing list