newbie

J

JASON JESSO

I'm trying to add a mode to a mkdir program a got off
the python cookbook.

The error I get is:
../mkdir.py jason 0777
Traceback (most recent call last):
File "./mkdir.py", line 31, in ?
_mkdir( sys.argv[1], sys.argv[2] )
File "./mkdir.py", line 25, in _mkdir
os.mkdir( newdir, mode )
TypeError: an integer is required

When I convert the mode from a string to an octal with
oct(int(sys.argv[2])) the permissions are all screwed
up.

Any help?

#!/usr/bin/env python

import sys, os

def _mkdir( newdir, mode ):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make
them as well
"""
if os.path.isdir( newdir ):
pass
elif os.path.isfile( newdir ):
raise OSError("a file with the same name as
the desired " \
"dir, '%s', already exists." %
newdir)
else:
head, tail = os.path.split( newdir )
if head and not os.path.isdir( head ):
_mkdir( head, mode )

if tail:
if mode is None:
os.mkdir( newdir )
else:
os.mkdir( newdir, mode )

if __name__ == '__main__' :
if len(sys.argv) < 3:
_mkdir( sys.argv[1], None )
else:
_mkdir( sys.argv[1], sys.argv[2] )
 
P

Peter Otten

JASON said:
I'm trying to add a mode to a mkdir program a got off
the python cookbook.

The error I get is:
./mkdir.py jason 0777
Traceback (most recent call last):
File "./mkdir.py", line 31, in ?
_mkdir( sys.argv[1], sys.argv[2] )
File "./mkdir.py", line 25, in _mkdir
os.mkdir( newdir, mode )
TypeError: an integer is required

When I convert the mode from a string to an octal with
oct(int(sys.argv[2])) the permissions are all screwed
up.

oct() converts an integer to the octal _string_ representation - that's why
python sees a string where an integer is required. Internally there is no
such thing as a decimal or an octal integer. Therefore you just have to
convert the octal string representation given on the command line to an
integer. This is done like so:

int(sys.argv[2], 8)

Here the second parameter is the base (the default being, of course, 10).

Peter
 
P

Paul Watson

JASON JESSO said:
I'm trying to add a mode to a mkdir program a got off
the python cookbook.

The error I get is:
./mkdir.py jason 0777
Traceback (most recent call last):
File "./mkdir.py", line 31, in ?
_mkdir( sys.argv[1], sys.argv[2] )
File "./mkdir.py", line 25, in _mkdir
os.mkdir( newdir, mode )
TypeError: an integer is required

When I convert the mode from a string to an octal with
oct(int(sys.argv[2])) the permissions are all screwed
up.

Specifying the base on int() suggested by Peter is probably the
best way. Here is another.

$ python
Python 2.1 (#1, May 23 2003, 11:43:56) [C] on aix4
Type "copyright", "credits" or "license" for more information.'0777'
 
W

wes weston

JASON said:
I'm trying to add a mode to a mkdir program a got off
the python cookbook.

The error I get is:
./mkdir.py jason 0777
Traceback (most recent call last):
File "./mkdir.py", line 31, in ?
_mkdir( sys.argv[1], sys.argv[2] )
File "./mkdir.py", line 25, in _mkdir
os.mkdir( newdir, mode )
TypeError: an integer is required

When I convert the mode from a string to an octal with
oct(int(sys.argv[2])) the permissions are all screwed
up.

Any help?

#!/usr/bin/env python

import sys, os

def _mkdir( newdir, mode ):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make
them as well
"""
if os.path.isdir( newdir ):
pass
elif os.path.isfile( newdir ):
raise OSError("a file with the same name as
the desired " \
"dir, '%s', already exists." %
newdir)
else:
head, tail = os.path.split( newdir )
if head and not os.path.isdir( head ):
_mkdir( head, mode )

if tail:
if mode is None:
os.mkdir( newdir )
else:
os.mkdir( newdir, mode )

if __name__ == '__main__' :
if len(sys.argv) < 3:
_mkdir( sys.argv[1], None )
else:
_mkdir( sys.argv[1], sys.argv[2] )

jason,
Your first instinct in direct problems like this
should be to get in the python interactive interpreter
and play around.


Python 2.3.3 (#1, Mar 11 2004, 22:02:41)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-20)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Note you still need to import.
Traceback (most recent call last):

wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top