Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > os.access() under windows

Reply
Thread Tools

os.access() under windows

 
 
Yann Leboulanger
Guest
Posts: n/a
 
      12-01-2007
Hi,

Under Windows XP os.access has a strange behaviour:

I create a folder test under e:

then os.access('e:\\test', os.W_OK) returns True. Everything's ok.

Now I move My Documents to this e:\test folder

Then os.access('e:\\test', os.W_OK) returns False !!

but this works:
f = open('e:\\test\\test', 'a')
f.write('abc')
f.close()

So why os.access returns False ?

--
Yann
 
Reply With Quote
 
 
 
 
David Tweet
Guest
Posts: n/a
 
      12-01-2007
To answer indirectly, usually the EAFP (easier to ask forgiveness than
permission) approach works better for this kind of thing.

try:
f = open('e:\\test\\test', 'a')
f.write('abc')
f.close()
except IOError:
print "couldn't write test file, continuing..."

On Dec 1, 2007 1:48 AM, Yann Leboulanger <(E-Mail Removed)> wrote:
> Hi,
>
> Under Windows XP os.access has a strange behaviour:
>
> I create a folder test under e:
>
> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>
> Now I move My Documents to this e:\test folder
>
> Then os.access('e:\\test', os.W_OK) returns False !!
>
> but this works:
> f = open('e:\\test\\test', 'a')
> f.write('abc')
> f.close()
>
> So why os.access returns False ?
>
> --
> Yann
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
-David
 
Reply With Quote
 
 
 
 
Yann Leboulanger
Guest
Posts: n/a
 
      12-02-2007
David Tweet wrote:
> To answer indirectly, usually the EAFP (easier to ask forgiveness than
> permission) approach works better for this kind of thing.
>
> try:
> f = open('e:\\test\\test', 'a')
> f.write('abc')
> f.close()
> except IOError:
> print "couldn't write test file, continuing..."
>
> On Dec 1, 2007 1:48 AM, Yann Leboulanger <(E-Mail Removed)> wrote:
>> Hi,
>>
>> Under Windows XP os.access has a strange behaviour:
>>
>> I create a folder test under e:
>>
>> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>>
>> Now I move My Documents to this e:\test folder
>>
>> Then os.access('e:\\test', os.W_OK) returns False !!
>>
>> but this works:
>> f = open('e:\\test\\test', 'a')
>> f.write('abc')
>> f.close()
>>
>> So why os.access returns False ?
>>
>> --
>> Yann
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

>



yep but the test file I try to create to know if I can write there may
be a folder for exemple, and the open() will fail.

So this can be considered as a bug in os module?
 
Reply With Quote
 
Martin v. Löwis
Guest
Posts: n/a
 
      12-03-2007
> I create a folder test under e:
>
> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>
> Now I move My Documents to this e:\test folder
>
> Then os.access('e:\\test', os.W_OK) returns False !!


This description is, unfortunately, too imprecise to allow reproducing
that effect. What precisely do you mean by "I move My Documents to
this e:\test folder"?

Regards,
Martin
 
Reply With Quote
 
Yann Leboulanger
Guest
Posts: n/a
 
      12-03-2007
Martin v. Löwis a écrit :
>> I create a folder test under e:
>>
>> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>>
>> Now I move My Documents to this e:\test folder
>>
>> Then os.access('e:\\test', os.W_OK) returns False !!

>
> This description is, unfortunately, too imprecise to allow reproducing
> that effect. What precisely do you mean by "I move My Documents to
> this e:\test folder"?
>



I Right click on "My Documents" folder, and change the path to e:\test
there.
So that "My documents" folder points to e:\test
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      12-03-2007
Yann Leboulanger wrote:
> Martin v. Löwis a écrit :
>>> I create a folder test under e:
>>>
>>> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>>>
>>> Now I move My Documents to this e:\test folder
>>>
>>> Then os.access('e:\\test', os.W_OK) returns False !!

>> This description is, unfortunately, too imprecise to allow reproducing
>> that effect. What precisely do you mean by "I move My Documents to
>> this e:\test folder"?
>>

>
>
> I Right click on "My Documents" folder, and change the path to e:\test
> there.
> So that "My documents" folder points to e:\test


Python uses the GetFileAttributesW API call to determine
access to the path you pass in. It then tests the return
against the FILE_ATTRIBUTE_READONLY flag and returns False
if you asked for Write and the Readonly flag is set.

The problem seems to be twofold: whereas a normal directory
(such as e:\temp) will not usually have its readonly flag set,
on a special directory such as the My Documents folder the flag
seems to be set by the system; also, the READONLY flag on a
directory refers to its deleteability, not to its writeablility.
At least, according to:

http://msdn2.microsoft.com/en-us/library/aa364944.aspx

I suspect this constitutes a bug in os.access which
should do some more interpretation of the results. But
no doubt Martin von L can comment with more authority
than I on this one.

TJG
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      12-03-2007
Tim Golden wrote:
> Yann Leboulanger wrote:
>> Martin v. Löwis a écrit :
>>>> I create a folder test under e:
>>>>
>>>> then os.access('e:\\test', os.W_OK) returns True. Everything's ok.
>>>>
>>>> Now I move My Documents to this e:\test folder
>>>>
>>>> Then os.access('e:\\test', os.W_OK) returns False !!
>>> This description is, unfortunately, too imprecise to allow reproducing
>>> that effect. What precisely do you mean by "I move My Documents to
>>> this e:\test folder"?
>>>

>>
>> I Right click on "My Documents" folder, and change the path to e:\test
>> there.
>> So that "My documents" folder points to e:\test

>
> Python uses the GetFileAttributesW API call to determine
> access to the path you pass in. It then tests the return
> against the FILE_ATTRIBUTE_READONLY flag and returns False
> if you asked for Write and the Readonly flag is set.
>
> The problem seems to be twofold: whereas a normal directory
> (such as e:\temp) will not usually have its readonly flag set,
> on a special directory such as the My Documents folder the flag
> seems to be set by the system; also, the READONLY flag on a
> directory refers to its deleteability, not to its writeablility.
> At least, according to:
>
> http://msdn2.microsoft.com/en-us/library/aa364944.aspx
>
> I suspect this constitutes a bug in os.access which
> should do some more interpretation of the results. But
> no doubt Martin von L can comment with more authority
> than I on this one.
>
> TJG


And just to complicate matters, it seems that readonly on
a folder has an entirely special meaning. At least, according
to this:

http://support.microsoft.com/kb/326549

Goodness knows what we're supposed to do with that.
Part of the issue here is that we're using a Unix-style
access API against Windows-style filesystem semantics.
According to the man pages, it looks as though it basically
checks the standard security bits against whatever you're
asking for for your user. On Windows, the security semantics
are quite different and you have to make some kind of
decision as to what the os.access means.

I'm happy to contribute a doc patch if I can imagine what
exactly to write.

TJG
 
Reply With Quote
 
Yann Leboulanger
Guest
Posts: n/a
 
      12-03-2007
Tim Golden a écrit :
>
> I'm happy to contribute a doc patch if I can imagine what
> exactly to write.
>


"Don't use it under windows, always consider it's True"?

Maybe it would be a good idea to remove support for windows for this
function, or make it always return True?
Current behaviour is worth that nothing, access() return False but
open() doesn't fail ...

--
Yann
 
Reply With Quote
 
Yann Leboulanger
Guest
Posts: n/a
 
      12-03-2007
Tim Golden a écrit :
>
> I'm happy to contribute a doc patch if I can imagine what
> exactly to write.
>


"Don't use it under windows, always consider it's True"?

Maybe it would be a good idea to remove support for windows for this
function, or make it always return True?
Current behaviour is worth that nothing, access() return False but
open() doesn't fail ...

--
Yann
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      12-03-2007
Yann Leboulanger wrote:
> Tim Golden a écrit :
>>
>> I'm happy to contribute a doc patch if I can imagine what
>> exactly to write.
>>

>
> "Don't use it under windows, always consider it's True"?


Well, that's not the case for files: if you set your
file's readonly attribute to True, then os.access (W_OK)
will return False and you won't be able to write to the
file:

<code>
import os
open ("temp.tmp", "w").close ()
os.access ("temp.tmp", os.W_OK)
os.system ("attrib +r temp.tmp")
os.access ("temp.tmp", os.W_OK)
open ("temp.tmp", "w").close ()
os.system ("attrib -r temp.tmp")
os.access ("temp.tmp", os.W_OK)
</code>

> Maybe it would be a good idea to remove support for windows for this
> function, or make it always return True?


The only issue (at least, the only one we're discussing here) is:
If os.W_OK on a directory returns True, that won't stop you writing
into that directory.

> Current behaviour is worth that nothing, access() return False but
> open() doesn't fail ...


To be precise: open () on a file within that directory doesn't fail.

Personally, I sympathise with you here. Python comes from a Unix
background and, unsurprisingly, it offers all the major Unix
system calls. Since Windows historically offered a Posix layer
which mapped them to *something*[1], the developers simply called
those under the covers. And the basic policy was: whatever Windows
passes back to Python, Python passes on to you.

Later (mid-2006, I think) some or all of these Posix-layer functions
were replaced by native Win32 APIs. At that point, it arguably became
Python's responsibility to define semantics. But it's a fuzzy sort of
area. I think a doc patch which said something like: "Calls
FileGetAttribute[A|W] and compares against FILE_READONLY_ATTRIBUTE"
might meet the case, although a bit of a cop-out.

TJG

[1] http://msdn2.microsoft.com/en-us/lib...dy(VS.80).aspx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A router running under WinXP runs under Windows Vista too? Peter Wagner Wireless Networking 12 02-04-2008 11:02 PM
Tomcat 5.5+ On a Mac, Under Eclipse, Under OS X Edward V. Berard Java 4 04-04-2006 05:14 AM
help : my jar file is not running under linux terminal , but it runs under JbuilderX ide bronby Java 1 07-15-2005 07:23 AM
[newbie]How to install python under DOS and is there any Wxpython can be installed under dos? john san Python 19 02-18-2005 12:05 PM
Java application developped under Linux running ridiculously slow under Windows hshdude Java 12 11-04-2004 05:49 PM



Advertisments