Returning the path of a file

Steve Holden sholden at holdenweb.com
Fri Jan 5 10:47:33 EST 2001


Daniel Klein <danielk at aracnet.com> wrote in message
news:mjob5tgge328h1sj94elg4b8sldrtteeus at 4ax.com...
> I've search the Beazley book as well as the Python reference materials
> and can't find answers to these two basically simple questions:
>
> 1) How to return the path of a file previously opened in read-only
> mode?
>
> myfile = open("foo")
>
> I know I can do something like
>
> os.getcwd()
>
> and this is where 'myfile' is, but if the file was opened by some
> other method, I would like to be able to interrogate where it was
> opened from.
>
File objects have a name attribute, which you can access to obtain the
parameter given to open(), but you may then need to construct the full path
using os.curdir() and os.path operations.

>>> os.chdir("c:\\windows")
>>> f = open("newfile.txt")
>>> f.name
'newfile.txt'

>
> 2) Is there a builtin or method to delete a file or do I have to
> execute an os process to do this?
>
remove() or unlink() will both take a path and delete the associated file.
removedirs() and rmdir() operate on directories.

>
> experiencing-python-growing-pains-ly y'rs,
> Daniel Klein
> Portland, OR USA
>
>
>






More information about the Python-list mailing list