strange interaction between open and cwd

B

Baz Walter

Python 2.6.4 (r264:75706, Mar 7 2010, 02:18:40)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):

can anybody explain how python is able to read the file at the end of
this session? i'm guessing it's a platform specific thing as i'm pretty
sure the above sequence of commands wouldn't work on windows (i.e.
attempting to remove the cwd would produce an error). but how can python
determine the parent directory of a directory that no longer exists?

this actually caused a bug for me. i was trying to ensure that my
program always resolved any file-names given on the command line by
using os.path.realpath(). i had assumed that if realpath failed, then
open would also fail - but not so!
 
P

Peter Otten

Baz said:
attempting to remove the cwd would produce an error). but how can python
determine the parent directory of a directory that no longer exists?

My tentative explanation would be that the directory, namely the inode,
still exists -- only the entry for it in its parent directory is gone.

So "one level up from here" is still a valid operation, but there is no
longer a path in the file system associated with "here".

Peter
 
G

Grant Edwards

Python doesn't determine the parent directory. The Unix system call
and filesystem code do.

The same thing happens in bash. IOW, you can do this:

$ rm -rf $PWD; cd ..
My tentative explanation would be that the directory, namely the
inode, still exists -- only the entry for it in its parent directory
is gone.

That sounds right.
So "one level up from here" is still a valid operation, but there is
no longer a path in the file system associated with "here".

I believe that is the case.
 
B

Baz Walter

My tentative explanation would be that the directory, namely the inode,
still exists -- only the entry for it in its parent directory is gone.

So "one level up from here" is still a valid operation, but there is no
longer a path in the file system associated with "here".

so "here" must always be available somehow, even if getcwd() fails
(something like the environment variable $PWD). shame that
os.getenv('PWD') isn't reliable, as it would solve my issue :(
 
G

Grant Edwards

so "here" must always be available somehow,
Yes.

even if getcwd() fails

If the current working directory doesn't _have_ a path within a
filesystem, what do you expect it to do?
(something like the environment variable $PWD). shame that
os.getenv('PWD') isn't reliable, as it would solve my issue :(

I don't understand what you mean by that.
 
B

Baz Walter

If the current working directory doesn't _have_ a path within a
filesystem, what do you expect it to do?

well, i expect it to fail, like i said :)
I don't understand what you mean by that.

i'm trying to understand how the path of the cwd can be known if there
is no entry for it in the filesytem - but this is starting to get a
little OT, so i won't pursue it here any longer.
 
G

Grant Edwards

i'm trying to understand how the path of the cwd can be known if there
is no entry for it in the filesytem

It can't. That's why cwd() failed.
- but this is starting to get a little OT, so i won't pursue it here
any longer.

OK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top