views:

717

answers:

4

Hello all again :D

I'm (trying) to create a folder and create a file within it.

Whenever i create that folder (via Python), it creates a folder that gives me no permissions at all and read-only mode.

When i try to create the file i get an IOError.

Erro:  <type 'exceptions.IOError'>

I tried creating (and searching) for a description of all other modes (besisdes 0770).

Can anyone give me light? What are the other modes codes?

+4  A: 

After you create the folder you can set the permissions with os.chmod

The mod is written in base 8, if you convert it to binary it would be

000 111 111 000
    rwx rwx rwx

The first rwx is for owner, the second is for the group and the third is for world

r=read,w=write,x=execute

The permissions you see most often are
7 read/write/execute - you need execute for directories to see the contents
6 read/write
4 readonly

When you use os.chmod it makes most sense to use octal notation so

os.chmod('myfile',0o666)  # read/write by everyone
os.chmod('myfile',0o644)  # read/write by me, readable for everone else

Remember I said you usually want directories to be "executable" so you can see the contents.

os.chmod('mydir',0o777)  # read/write by everyone
os.chmod('mydir',0o755)  # read/write by me, readable for everone else

Note: The syntax of 0o777 is for Python 2.6 and 3+. otherwise for the 2 series it is 0777. 2.6 accepts either syntax so the one you choose will depend on whether you want to be forward or backward compatible.

gnibbler
"windows"-tag..
Filip Ekberg
I guess he meant os.chmod
RedGlyph
Give the guy an example too, it can be confused with *nix chmod.
Filip Ekberg
Normally, octal is indicated by 0666, 0444...not the 0o666 notation.
Jonathan Leffler
@Jonathan, that is a syntax error in Python3. The 0oNNN notation was preferred as it is more explicit. 0oNNN works in Python2 as well as Python3
gnibbler
`0oNNN` works in ≥2.6. `0NNN` works in <3.0. 2.5 doesn't understand the new syntax, 3.0 doesn't understand the old syntax.
ephemient
@ephemient Thanks for the clarification
gnibbler
This 0o00 syntax is stupid and it contradicts the 2.6 docs: http://docs.python.org/library/os.html#os.makedirs
Noah
@Noah, There is no contradiction that is just an example of how to use `os.makedirs` see http://docs.python.org/reference/lexical_analysis.html#integer-and-long-integer-literals. The `os.makedirs` example has been updated for Python3 series http://docs.python.org/3.1/library/os.html
gnibbler
+3  A: 

You've probably got a funky umask. Try os.umask(0002) before making your directory.

Jonathan Feinberg
A umask of 777 would ensure that all permission bits are zero. You might have meant 000; it might be more sensible to use 002 or 022.
Jonathan Leffler
/me is an idiot. Thanks.
Jonathan Feinberg
A: 

The Python manual says:

os.mkdir(path[, mode])

Create a directory named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out. Availability: Unix, Windows.

Have you specified a mode - which mode did you specify. Did you consider specifying a mode explicitly? And what is the program's umask value set to"

Jonathan Leffler
A: 

Since your on Windows, this might be a crapshoot. Make sure there aren't any wacky special permissions on the parent directory or with the policy settings that defines the permissions any directories created by your account get. I doubt this is a python problem as I haven't been able to recreate the problem on Windows with a relatively vanilla Vista install.

whaley