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

Robert Kern rkern at
Sun Jul 31 09:36:16 CEST 2005

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)?

I don't understand. The leading zero only exists in a particular string 
representation. os.chmod() needs an integer, not a string. 0777 == 511.

os.chmod('myfile', 0777)
os.chmod('myfile', 511)
os.chmod('myfile', int('777', 8))

They all do *exactly* the same thing. End of story.

If you really need a string representation in octal (os.chmod() 
doesn't), then use oct() on the integer.

Robert Kern
rkern at

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter

More information about the Python-list mailing list