Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

windows - Python mkdir giving me wrong permissions

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.

Error:  <type 'exceptions.IOError'>

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...