在 Python 中使用 os.makedirs 创建目录时的权限问题 [英] Permission problems when creating a dir with os.makedirs in Python
问题描述
我只是想处理上传的文件并将其写入工作目录中,该目录的名称是系统时间戳.问题是我想以完全权限 (777) 创建该目录,但我不能!使用以下代码创建具有 755 权限的目录.
I'm simply trying to handle an uploaded file and write it in a working dir which name is the system timestamp. The problem is that I want to create that directory with full permission (777) but I can't! Using the following piece of code the created directory with 755 permissions.
def handle_uploaded_file(upfile, cTimeStamp):
target_dir = "path_to_my_working_dir/tmp_files/%s" % (cTimeStamp)
os.makedirs(target_dir, mode=0777)
推荐答案
根据python官方文档 os.makedirs
函数的 mode 参数在某些系统上可能会被忽略,而在没有被忽略的系统上,当前的 umask 值会被屏蔽掉.
According to the official python documentation the mode argument of the os.makedirs
function may be ignored on some systems, and on systems where it is not ignored the current umask value is masked out.
无论哪种方式,您都可以使用 os.chmod
函数将模式强制为 0o777(0777 抛出语法错误).
Either way, you can force the mode to 0o777 (0777 threw up a syntax error) using the os.chmod
function.
这篇关于在 Python 中使用 os.makedirs 创建目录时的权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!