newbie

Peter Otten __peter__ at web.de
Thu Aug 19 16:55:47 CEST 2004


JASON JESSO wrote:

> 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




More information about the Python-list mailing list