python mkdir权限 window,Python mkdir给我错误的权限

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:

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值