473,560 Members | 2,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('myfil e',0777)?

I know the leading zero is important in os.chmod.

KB

Jul 31 '05 #1
5 15158
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('myfil e',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('myfil e', 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('myfil e', 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('myfil e', 0777)
os.chmod('myfil e', 511)
os.chmod('myfil e', 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('myfil e', 0777)?


os.chmod('myfil e', 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
199224
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 each character and test it? or is there an easier way? Thanks. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system...
3
27548
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
9661
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, etc in .NET? s1.trim OR trim(s1) s1.length OR len(s1) Thank you!
5
3421
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: unsupported operand type(s) for /=: 'unicode' and 'int' I want to divide x by 10 and assign that value back to x.
2
2031
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 dynamic type language, should python convert it for me automatically? count = sys.argv for i in range(count): #do some stuff
4
4813
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 (ds.Tables.Rows.Count > 0) { s1 = ds.Tables.Rows.Count;
1
3964
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 position.
9
8233
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
6406
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 store in a hash table .. each string is of the form "rlegrEgabc---supprod-C7M" , and all of them are unique.
0
7631
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8063
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7595
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3605
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1168
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.