Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to convert a string like '777' to an octal integer like 0777?

Reply
Thread Tools

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

 
 
KB
Guest
Posts: n/a
 
      07-31-2005
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.

KB

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      07-31-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",

511 511
>>>


Cheers,
John
 
Reply With Quote
 
 
 
 
KB
Guest
Posts: n/a
 
      07-31-2005
Thanks, John.

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

 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      07-31-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', )

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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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

 
Reply With Quote
 
KB
Guest
Posts: n/a
 
      07-31-2005
> The leading zero only exists in a particular string
> representation. os.chmod() needs an integer, not a string. 0777 == 511.


Thanks, Robert.

What you said is exactly what I did not understand clearly,
because I am just a beginner in Python programming.

KB

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-31-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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Efficiently convert characters to octal representation Worky Workerson Perl Misc 11 07-29-2006 03:02 AM
Converting negative integer to octal/hexadecimal jaks.maths@gmail.com C Programming 15 06-23-2006 12:06 PM
Convert potion of string to Octal Bill H Perl Misc 10 01-04-2006 12:02 AM



Advertisments