473,699 Members | 2,534 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 15179
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
199259
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 (http://www.grisoft.com). Version: 6.0.720 / Virus Database: 476 - Release Date: 7/14/04
3
27562
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
9681
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
3430
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
2036
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
4815
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
3973
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
8240
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
6428
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
8631
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7787
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4641
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2366
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.