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

Discussion in 'Python' started by KB, Jul 31, 2005.

  1. KB

    KB Guest

    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
    KB, Jul 31, 2005
    #1
    1. Advertising

  2. KB

    John Machin Guest

    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
    John Machin, Jul 31, 2005
    #2
    1. Advertising

  3. KB

    KB Guest

    Thanks, John.

    But my point is how to keep the leading zero in 0777,
    in order to be used in os.chmod('myfile', 0777)?
    KB, Jul 31, 2005
    #3
  4. KB

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jul 31, 2005
    #4
  5. KB

    KB Guest

    > 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
    KB, Jul 31, 2005
    #5
  6. 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.
    Steven D'Aprano, Jul 31, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    15
    Views:
    11,922
    Eric Sosman
    Jun 23, 2006
  2. Aron Griffis

    -0777 broken in 1.8.1?

    Aron Griffis, Feb 4, 2004, in forum: Ruby
    Replies:
    6
    Views:
    115
    Aron Griffis
    Feb 7, 2004
  3. Pat Kiatchaipipat

    how to set permission '777' to dir?

    Pat Kiatchaipipat, Feb 12, 2010, in forum: Ruby
    Replies:
    6
    Views:
    259
    Albert Schlef
    Feb 16, 2010
  4. javatiger

    Newbie 777/ 755 only allowed on server

    javatiger, Sep 7, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    80
    Gunnar Hjalmarsson
    Sep 7, 2004
  5. Bill H

    Convert potion of string to Octal

    Bill H, Jan 3, 2006, in forum: Perl Misc
    Replies:
    10
    Views:
    229
    Bill H
    Jan 4, 2006
Loading...

Share This Page