newbie - integer variable with leading zero for os.makedirs

james jtoher at cyllene.uwa.edu.au
Tue Dec 2 00:12:57 EST 2003


I'm using python 2.3 and 2.2 on Mac OSX 10.3.1

My script creates directories from its input using os.makedirs 

This from http://www.python.org/doc/2.3.2/lib/os-file-dir.html:

  makedirs(  path[, mode])
  Recursive directory creation function. Like mkdir(), but makes
  all intermediate-level directories needed to contain the leaf
  directory...
  The default mode is 0777 (octal)...

My text input specifies the mode as a string - '750', for example.  I
want to convert the string input to an integer variable for use by
os.makedirs.

makedirs works fine when I use a literal like 0750, but my problem is
setting a variable to that kind of value.  int('0750') converts the
string from ascii to integer, but loses the first zero padding. 
Without the leading zero os.makedirs makes a mess of the permissions
when it creates the path.

I've tried:

permission = int('750', 8)
mode = oct(permission)

but this returns an octal string '0755' and os.makedirs barfs again. 
It really wants an integer and it really wants that leading zero.  How
can I assign a number with a leading zero to an integer variable?

Any thoughts much appreciated.




More information about the Python-list mailing list