How to convert a string like '777' to an octal integer like 0777?

John Machin sjmachin at lexicon.net
Sun Jul 31 01:39:23 EDT 2005


KB wrote:
> Hi,
> 
> This may be a rudimentary question:
> 
> How to convert a string like '777' to an octal integer like 0777,
> so that it can be used in os.chmod('myfile',0777)?
> 
> I know the leading zero is important in os.chmod.

There is no law that says constant arguments to os.chmod have to be 
expressed in octal -- it's just a historical accident that it's 
convenient (for octal grokkers, anyway): there are 3 permissions (rwx) 
and 2 ** 3 == 8.

Consider the following, whcih should provide enlightenment as well as 
answer your question:

 >>> print 0777, int("777", 8)
511 511
 >>>

Cheers,
John



More information about the Python-list mailing list