[TriZPUG] identity crisis solved

Laura Tateosian lgtateos at ncsu.edu
Thu Sep 5 16:47:52 CEST 2013


Thanks to all.  abspath will do the trick!

On 9/4/2013 7:50 PM, Philip Semanchuk wrote:
> On Sep 4, 2013, at 6:10 PM, Steve Gambino wrote:
>
>> Laura,
>>
>> import os
>> os.getcwd()  # will return the current working directory
> This is kind of off-topic, but I assumed that os.getcwd() was always safe to call until I recently burned myself. Check this out --
>
>>>> import os
>>>> import tempfile
>>>> tempdir = tempfile.mkdtemp()
>>>> os.chdir(tempdir)
>>>> os.rmdir(tempdir)
>>>> os.getcwd()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> OSError: [Errno 2] No such file or directory
> The real-world scenario for this is that my Web app was building a zip file on the fly in a temp directory in response to a Web request and then deleting the temp directory as the last step in its processing. The next bit of code that called os.getcwd() got clobbered with the OSError. (In my case it was when I first imported matplotlib, and it took me a while to figure out what was "wrong" with matplotlib.)
>
> Happy Pythoning,
> Philip
>
>
>> On Wed, Sep 4, 2013 at 5:10 PM, Laura Tateosian <lgtateos at ncsu.edu> wrote:
>>
>>> I'd like to get the path of the current file, so that I can append the
>>> sys.path and then import user-defined modules based on relative paths.
>>>
>>> sys.argv[0] can do this. But sometimes it returns just the file base name
>>> or an empty string, depending on how the script is run. The same goes for
>>> __file__
>>>
>>> Is there a simple reliable approach for determining the full path file
>>> name of the script I'm inside?
>>>
>>> Thanks,
>>> Laura
>>> ______________________________**_________________
>>> TriZPUG mailing list
>>> TriZPUG at python.org
>>> https://mail.python.org/**mailman/listinfo/trizpug<https://mail.python.org/mailman/listinfo/trizpug>
>>> http://trizpug.org is the Triangle Zope and Python Users Group
>>>
>> _______________________________________________
>> TriZPUG mailing list
>> TriZPUG at python.org
>> https://mail.python.org/mailman/listinfo/trizpug
>> http://trizpug.org is the Triangle Zope and Python Users Group
> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://trizpug.org is the Triangle Zope and Python Users Group



More information about the TriZPUG mailing list