Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Possible bug in setting Windows file permissions? (os.chmod() not working properly?)

12 views
Skip to first unread message

Matt Shomphe

unread,
Jun 16, 2003, 7:17:41 PM6/16/03
to
I noticed an error when trying to change the file permissions for a
file on a Windows (2K) machine. I'm using David Beazley's excellent
"Python Essential Reference, 2nd. Ed." as a reference. According to
the book (p. 186 for those following along at home), it should be used
thusly:

import os
os.chmod('myReadOnlyFile.txt', os.O_RDWR)


Where for os.O_RDWR, you could have any of the modes specified on p.
184. In other words, all the same bitwise-or modes for opening a
file. What happens with the above code is: nothing at all. However,
the following code *will* work to change a file to be accessible:

import os
os.chmod('myReadOnlyFile.txt', 33206)

So, either I am using the os.chmod() function incorrectly, or there is
something wrong elsewhere.

Any hints on what's to be done?

M@

Gus Tabares

unread,
Jun 16, 2003, 7:29:48 PM6/16/03
to
Matt,

I do believe os.O_RDWR is specifically for the open() function. Check out
the bottom of this page:
http://www.python.org/doc/current/lib/os-fd-ops.html

HTH,
Gus

M@
--
http://mail.python.org/mailman/listinfo/python-list


Gus Tabares

unread,
Jun 16, 2003, 7:49:43 PM6/16/03
to
Matt,

The second parameter to os.chmod() is typical UNIX-style permissions (i.e,
777 is r/w/x). I would suggest firing up google and searching for UNIX
permissions (or something of the sort). I'm pretty positive that
os.chmod(file, 777) will make <file> R/W.


HTH,
Gus

-----Original Message-----
From: Matthew Shomphe [mailto:Matt...@heyanita.com]
Sent: Monday, June 16, 2003 7:44 PM
To: Gus Tabares
Cc: pytho...@python.org
Subject: RE: Possible bug in setting Windows file permissions?
(os.chmod() not working properly?)


Gus,

Thanks for the information. I thought this was the case as well. However,
in the book (below), it says:

----------------------------------------------------
chmod(path, mode)

Changes the mode of path. mode has the same values as described for the
open() function. UNIX and Windows.
----------------------------------------------------

Beazley could full well be wrong (sorry, David! ;). In the on-line
documentation (http://www.python.org/doc/current/lib/os-file-dir.html) it
says:

------------------------------------------------------------------------
chmod(path, mode)
Change the mode of path to the numeric mode. Availability: Unix, Windows.
------------------------------------------------------------------------

However, I don't see a description of the values for "mode". I could only
deduce the numeric value required to get read/write permissions by checking
the permissions of a file that I knew to be both readable & writable:

=========================
from stat import *
print os.stat('myReadAndWriteFile.txt')[ST_MODE]
=========================

I can use the numeric value I grabbed from the above output to set
permissions. However, setting permissions using these raw numeric values
strikes me as A Bad Thing(TM). It doesn't make the code portable. And who
knows what Redmond will do in the future?

Let me make my inquiry clearer (I hope):
(1) What are the values expected for the second argument to os.chmod()?
(2) Are these values platform independant?
(3) Is it documented anywhere?

Thanks for your help!

M@

PS. I'm using python2.3b1

Matthew Shomphe

unread,
Jun 16, 2003, 7:59:21 PM6/16/03
to
Bloody hell, you're right! I didn't think that UNIX file permission descriptions would work in Windows, but they do. Can someone make this clearer in the documentation?

It's interesting to note that '666' maps into '33206' in the Windows file permission description. So you can (apparently), use both values to make a file readable & writeable.

Thanks Gus, you rock!

M@

-----Original Message-----
From: Gus Tabares [mailto:gus.t...@verizon.net]
Sent: Monday, June 16, 2003 4:52 PM
To: Gus Tabares; Matthew Shomphe
Cc: pytho...@python.org
Subject: RE: Possible bug in setting Windows file permissions?
(os.chmod() not working properly?)


OK I lied. It's 666:)


Sorry,
Gus


Matt,


HTH,
Gus


Gus,

----------------------------------------------------
chmod(path, mode)

Thanks for your help!

M@


Matt,

HTH,
Gus

M@
--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list


Tim Peters

unread,
Jun 16, 2003, 7:41:54 PM6/16/03
to
[Matt Shomphe]

> I noticed an error when trying to change the file permissions for a
> file on a Windows (2K) machine. I'm using David Beazley's excellent
> "Python Essential Reference, 2nd. Ed." as a reference. According to
> the book (p. 186 for those following along at home), it should be used
> thusly:
>
> import os
> os.chmod('myReadOnlyFile.txt', os.O_RDWR)

That's hard to believe. O_RDWR is only for use with the os.open() function
(note well too: not the builtin open() function).

Try this:

os.chmod('myReadOnlyFile.txt', 0666)

The leading zero is important. If that still doesn't work, it could be you
don't have permission to change permissions in the current directory. The
Win2K permission system is much more elaborate than the simple Unix-ish
model Python exposes here.


Gus Tabares

unread,
Jun 16, 2003, 7:51:54 PM6/16/03
to


Sorry,
Gus


Matt,


HTH,
Gus


Gus,

----------------------------------------------------
chmod(path, mode)

Thanks for your help!

M@


Matt,

HTH,
Gus

I noticed an error when trying to change the file permissions for a
file on a Windows (2K) machine. I'm using David Beazley's excellent
"Python Essential Reference, 2nd. Ed." as a reference. According to
the book (p. 186 for those following along at home), it should be used
thusly:

import os
os.chmod('myReadOnlyFile.txt', os.O_RDWR)

Where for os.O_RDWR, you could have any of the modes specified on p.
184. In other words, all the same bitwise-or modes for opening a
file. What happens with the above code is: nothing at all. However,
the following code *will* work to change a file to be accessible:

import os

Matthew Shomphe

unread,
Jun 16, 2003, 7:44:01 PM6/16/03
to

Peter Hansen

unread,
Jun 16, 2003, 8:27:55 PM6/16/03
to
Gus Tabares wrote:
>
> OK I lied. It's 666:)

In any case, aren't these supposed to be meaningful only as octal
values? So you'd need 0666? I'm not actually going by the docs
here, just memory. 777 and 666 would be extremely weird parameters
to pass in otherwise (i.e. as decimal).

-Peter

Tim Peters

unread,
Jun 16, 2003, 8:13:28 PM6/16/03
to
[Matthew Shomphe]

> Bloody hell, you're right! I didn't think that UNIX file
> permission descriptions would work in Windows, but they do.

Sometimes they do, and provided you stick to things that also make sense on
Windows (e.g., the "execute" bit doesn't mean beans on Windows, or, in the
other direction, the Windows "hidden" bit has no spelling via chmod).

> Can someone make this clearer in the documentation?

You're elected <0.5 wink>. Start by doing a google search on

_chmod msdn

The top hit will take you to MS's docs for MS's implementation of chmod
(which Python calls).

> It's interesting to note that '666' maps into '33206' in the
> Windows file permission description. So you can (apparently), use
> both values to make a file readable & writeable.

One of many undocumented mysteries. This one isn't too surprising:

>>> oct(33206)
'0100666'
>>>


Gus Tabares

unread,
Jun 16, 2003, 8:34:55 PM6/16/03
to
Peter,

os.chmod(file, 666) seems to work quite proficiently. It might be possible
that it is converted (as that is easy to do).


Gus

-----Original Message-----
From: python-l...@python.org
[mailto:python-l...@python.org]On Behalf Of Peter Hansen
Sent: Monday, June 16, 2003 8:28 PM
To: pytho...@python.org
Subject: Re: Possible bug in setting Windows file permissions?
(os.chmod() not working properly?)

-Peter
--
http://mail.python.org/mailman/listinfo/python-list


Peter Hansen

unread,
Jun 16, 2003, 10:14:58 PM6/16/03
to
Gus Tabares wrote:
>
> Peter,
> os.chmod(file, 666) seems to work quite proficiently. It might
> be possible that it is converted (as that is easy to do).

Possible, but unlikely, I think. Note that o666 and d666 share
the bits 10010010, which quite possibly includes the bits necessary
to cause Windows to make a file read-write...

Are you really willing to throw parameters at a low-level OS function
which "seem" to work, or would you rather avoid, say, accidentally
clearing the archive bit, or accidentally setting the HIDDEN bit? ;-)

-Peter

Gus Tabares

unread,
Jun 16, 2003, 10:26:11 PM6/16/03
to

Yeah I guess I see your point. I have and do use 666 quite often. I guess
adding a 0 wouldn't really hurt...

-----Original Message-----
From: python-l...@python.org
[mailto:python-l...@python.org]On Behalf Of Peter Hansen
Sent: Monday, June 16, 2003 10:15 PM
To: pytho...@python.org
Subject: Re: Possible bug in setting Windows file permissions?
(os.chmod() not working properly?)

-Peter
--
http://mail.python.org/mailman/listinfo/python-list


Matt Gerrans

unread,
Jun 17, 2003, 3:05:50 AM6/17/03
to
I thought that the the stat module was supposed to help for this kind of
thing, eg.:

os.chmod( filename, stat.S_IREAD )

Or was I just fooled because it looks plausible? It *seems* to do the
right thing.

Anyway, I have found that to full and reliable usage of file attributes in
Windows, it is necessary to shift to the platform-specific route an and use
the win32file module (GetFileAttributes(), SetFileAttributes(), plus
win32con's FILE_ATTRIBUTE_* constants).

- Matt

0 new messages