Python mkdir给我错误的权限 [英] Python mkdir giving me wrong permissions

查看:120
本文介绍了Python mkdir给我错误的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个文件夹并在其中创建文件.

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

每当我(通过Python)创建该文件夹时,它都会创建一个文件夹,该文件夹完全不授予我权限,并且为只读模式.

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

当我尝试创建文件时,出现IOError.

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

Error:  <type 'exceptions.IOError'>

我尝试创建(并搜索)所有其他模式的描述(0770以后).

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?

推荐答案

创建文件夹后,可以使用os.chmod

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

该mod以8为基数编写,如果将其转换为二进制,则为

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

000 111 111 000
    rwx 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

r=read,w=write,x=execute

您最常看到的权限是
7读/写/执行-您需要执行目录才能看到内容
6个读/写
4个只读

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

使用os.chmod时,使用八进制表示法最有意义 所以

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

注意:0o777的语法适用于Python 2.6和3+.否则,对于2系列,它是0777. 2.6接受任何一种语法,因此您选择哪种语法将取决于您是要向前还是向后兼容.

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.

这篇关于Python mkdir给我错误的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆