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


> I'm trying to add a mode to a mkdir program a got off
> the python cookbook.
> The error I get is:
> ./ jason 0777
> Traceback (most recent call last):
>   File "./", line 31, in ?
>     _mkdir( sys.argv[1], sys.argv[2] )
>   File "./", 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).


More information about the Python-list mailing list