v1shnu
v1shnu

Reputation: 2231

Permission of subfolders when using os.makedirs()

I am trying to create a directory using Python on UNIX.

This is my code:

import os, sys

if len(sys.argv) > 2:
    print "USAGE : createDir <codeset>"
    sys.exit()

rootName= sys.argv[1]

# setting $HOME and $DATA path
home_path = "/opt/app/" + rootName+ "/"
data_path = "/data/app/" + rootName + "/"

# creating rootName directory
os.makedirs(home_path , 0755)
os.makedirs(data_path , 0755)

# create data and log folder in data_path
os.makedirs(data_path + "data")
os.makedirs(data_path + "log")

# create J2SEDomain and scripts folders
os.makedirs(home_path + "J2SEDomain")
os.makedirs(home_path + "scripts")

print "All folders created successfully."

Now I want to know if the permission I set for the rootName folder applies for all the subfolders inside it or only for the rootName folder alone.

Also please tell me how to efficiently write the above code as I am new to Python.

EDIT: I am not checking the permission explicitly but asking what will be the default permission for the sub-folders inside a folder which has permission set to 0755

Upvotes: 0

Views: 899

Answers (1)

lurker
lurker

Reputation: 58274

I think it can be shortened a bit. You don't need the explicit 0755 I don't think, since that is the typical default. So I think these lines can simply be eliminated:

# creating rootName directory
os.makedirs(home_path , 0755)
os.makedirs(data_path , 0755)

The home_path and data_path will be created by the subsequent calls you have to os.makedirs.

However, if you really do need to deviate from default permissions on your home_path and data_path top levels, then you will need to keep those calls and set the permissions accordingly.

Upvotes: 1

Related Questions