Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission problems when creating a dir with os.makedirs in Python

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) 
like image 833
green69 Avatar asked Mar 08 '11 11:03

green69


People also ask

How do I give permission to a folder in Python?

chmod(path, 0444) is the Python command for changing file permissions in Python 2. x. For a combined Python 2 and Python 3 solution, change 0444 to 0o444 . You could always use Python to call the chmod command using subprocess .

Why is mkdir permission denied?

[ErrorException] mkdir(): Permission denied That means you do not have write permission on your project folder. Create a new folder, say 'myproject and run sudo chmod 777 myproject . Then move to 'myproject' folder and create project.

What is mode in Makedirs in Python?

The method makedirs() is recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory. The default mode is 0o777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out.

Will os makedirs overwrite?

Overwrite Directory Even ExistBy default, the makedirs() method will not overwrite if the provided directory exists currently.


1 Answers

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.

Either way, you can force the mode to 0o777 (0777 threw up a syntax error) using the os.chmod function.

like image 82
srgerg Avatar answered Sep 21 '22 09:09

srgerg