473,386 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

KB
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

Jul 31 '05 #1
5 15131
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
Jul 31 '05 #2
KB
Thanks, John.

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

Jul 31 '05 #3
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
rk***@ucsd.edu

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

Jul 31 '05 #4
KB
> 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

Jul 31 '05 #5
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.

Jul 31 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: dave | last post by:
I am reading input from a form. I want to validate the input by making sure that the string is actually an integer. How would I do this? Do i need to convert it to a character array and break down...
3
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
15
by: Teresa | last post by:
1) Should I use Integer.Parse to convert a string into an integer in .NET now? CType(sUserID, Integer) OR Integer.Parse(sUserID) 2) And is it better to use the string class to trim, get length,...
5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError:...
2
by: yinglcs | last post by:
Can you please tell me why the following code does not work in python? My guess is I need to convert 'count' from a string to an integer. How can I do that? And my understanding is python is a...
4
by: Shaileshsharma | last post by:
Hi, I have a value in string type variable ,i want to convert the string type into integer.This is my sample code int s1; string s2; int s3; if...
1
by: shyamal | last post by:
can anyone pls help me in visual basic to convert string data to integer. I am getting encoder position pulse data through Rs232 as string like "-450". I need to calculate the actual displacement...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
3
by: aniketkadu2002 | last post by:
i have an array of string which had size of about 3000 Each entity in the array is a unique string . i would want to convert each of this string into a unique integer so that i can use it to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.