0

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

v1shnu
  • 2,211
  • 8
  • 39
  • 68
  • possible duplicate of [Checking File Permissions in Linux with Python](http://stackoverflow.com/questions/1861836/checking-file-permissions-in-linux-with-python) – Torxed Mar 13 '15 at 12:49
  • The permissions that are set for file/folder creation from the shell for whatever user is executing the script should apply by default. If those permissions are satisfactory, you shouldn't have to explicitly set them. Have you tried it and are you getting something unexpected? – lurker Mar 13 '15 at 12:49
  • `print(os.stat(filepath))` – Torxed Mar 13 '15 at 12:49
  • @lurker No, I do not have access to try this code on the server. I just want to know what would be the permission of the folders inside the home_path and data_path folders. Since I set the using `os.makedirs(home_path,0755)` , I want to know if the folders created inside them also have the same permission set. – v1shnu Mar 13 '15 at 12:52
  • Ofc they do. they have direct heritage of their parent folders. – Torxed Mar 13 '15 at 12:55
  • I didn't mention a server. I'm not sure where that comes into play here since you haven't mentioned it before. But, the permissions, as I say, are whatever the login shell of the executing user are setup for. If you're logged in as that user, you could enter `umask` to see what would not be set. – lurker Mar 13 '15 at 12:55
  • On the efficiency question, you have just a little straight-line code here creating a few different folder paths. I don't see how you would shorten it. The code is nicely organized and clear as well. – lurker Mar 13 '15 at 12:56
  • @Torxed that is what I wanted to know. So the folders inside it will have the permission as 0755 which is the permission of their parent folder ? – v1shnu Mar 13 '15 at 13:05
  • @lurker I was answering your question that whether this code gave unexpected output. Thanks ! Will creating a function which takes 2 args ( path ,name ) make the code more simpler , so that I can skip writing os.makedirs for each folder ? – v1shnu Mar 13 '15 at 13:05
  • **EDIT** comment of mine has the question which I actually wanted to ask. Just did not know how to ask the question . Sry. – v1shnu Mar 13 '15 at 13:05

1 Answers1

1

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.

lurker
  • 56,987
  • 9
  • 69
  • 103