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

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jul 31 07:05:56 EDT 2005


On Sun, 31 Jul 2005 00:24:08 -0700, KB wrote:

> Thanks, John.
> 
> But my point is how to keep the leading zero in 0777,
> in order to be used in os.chmod('myfile', 0777)?

os.chmod('myfile', 0777)

Python will recognise integers written in octal if you leave a
leading zero, and in hex if you use a leading 0x or 0X.

>>> 010
8
>>> 0x10
16
>>> 010 + 0x10
24

As John pointed out, you don't have to use octal for chmod. You can use
decimal, or hex -- anything that is an integer.

-- 
Steven.




More information about the Python-list mailing list