[Tutor] (no subject) [Escape codes / os.access()]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 15 Dec 2001 18:30:34 -0800 (PST)


On Sat, 15 Dec 2001, Jean Montambeault wrote:

>     Hmmmmm! I had a little time on my hands and tried this :
> >>> os.access(r"c:\essai.py", os.R_OK)
> 1
> >>> os.access(r"c:\essai.py", os.W_OK)
> 1
> 
> and it works, as you can see,  but:
> 
> >>> os.access("c:\essai.py", os.W_OK)
> 1
> >>> os.access("c:\essasdafis.py", os.W_OK)
> 0
> >>>
> 
> works too so the os module takes care of the backslash problem as it seems.

Actually, os.access() doesn't.  In the particular case of figuring out
what "\e" means, Python sees that it doesn't know any escape code like
that, so it will let it through.

Python 2.1 shows this pretty well if we use the interpreter:

###
>>> "\e"
'\\e'
>>> "\n"
'\n'
###

In the first call, Python will automagically escape the backslash for
us.  That's why it shows the '\\e' in there, just to point out that
there's a literal backslash in the string.

In the second call, there is an escape code called '\n', which is the
"newline" character.  So that's why its representation looks different
from '\e'.

If your files began with an 'n' or 't', you would probably run into this
problem more often.  One way to get around worrying about backslashes is
to dodge them altogether, and use forward slashes '/' instead.  Windows
will understand that we're using the forward slashes to get inside
directories.  So this should work:

###
os.access("c:/essai.py", os.W_OK)
###


> It wouldn't work for :
>
> rich=open("c:\my file\ my beautiful file", "w")
>
> you must use the "r" or "\\" with open() there.
> That way I assumed that it would be the same for "os.access()"
> and now I'm consumed by shame. :o(


No no, you were right the first time.  Handing backslashes in string
literals should be consistant throughout Python.  open() doesn't do
anything more special with its input than os.access().  There may be
another reason why your open():

> rich=open("c:\my file\ my beautiful file", "w")

failed.  If you can show us the error message, we may get an idea of why
it didn't work.  One thing: can files begin with spaces on Windows?  I
don't have a Windows system at the moment to check this.


Talk to you later!