Python: How can I print bytes? - python

I'm using Python 2.7.
I want to print a binary in decimal, but I'm receiving an error, which I do not understand.
Eg. I am trying:
print 0b111
I am expecting 7. But it returns:
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no "print" mailcap rules found for type "text/x-python"
Can you help? Just a beginner with Python!

... at /usr/bin/print ...
Sounds like you're invoking the script incorrectly. Either use a shebang that points to a Python executable or explicitly pass it to the executable.
python somescript.py

Related

subprocess.call path problem , how to fix?

my subprocess.call problem is that my shortcut target is with extra INI file which is LIV2.INI and my exe file should run whit it . and my target link in shortcut looks like this
"C:\Program Files (x86)\AMO\EXE\PROGRAM LIVE 2.exe" LIV2.INI
i tried this
subprocess.call('"C:\Users\admin\Desktop\PROGRAM LIVE 2.exe" LIV2.INI')
and i tried this
subprocess.call('C:\Users\admin\Desktop\PROGRAM LIVE 2.exe LIV2.INI')
and i still get error that the ini file missing ? How can i fix this :)
THank you in advance
ERROR : INI FILE Missing or Wrong Name
Please also edit your question to actually include the error since you will get a syntax error, not an error that the ini file is missing.
You have two issues here, first you have a syntax error since "\Us" is not a valid string in python. \u marks the start of a Unicode escape sequence and the character S is not a valid unicode escape key. You can fix this by using double \\ to escape the \ character and tell python you want your string to include a \ and not use it as the start of a escape sequence.
Secondly, subprocess.call excpects a list, not a string (unless you set shell=True; but don't do that, since it means you have to manually escape things which you have already discovered is hard). The first element of the list
is the executable to run and the rest are command line arguments. For example if you wanted to run pythoneand print "hello world" you would type:
subprocess.call(['python', '-c', 'print ("hello world")'])
Notice the missing quotes around the python string? You don't need those since the command line arguments are passed in raw and no shell will attempt to split them if you don't include quotes.
Putting it all together creates something like this:
subprocess.call(['C:\\Users\\admin\\Desktop\\PROGRAM LIVE 2.exe', 'LIV2.INI'])
Notice the double backslashes and how each command line argument is its own list element.

Ignore evaluation of '\n' character in sys.argv

Oddly enough, when running this program with the arguments of
program.py "(lp0\nS'cat'\np1\naI5\na."
With program.py being:
import sys,pickle
print sys.argv[1]=="(lp0\nS'cat'\np1\naI5\na."
False is printed... I have narrowed the difference in evaluation to the \n character however I can find no way of ignoring such.
Why is this and how can I fix it?
You need to use raw string literal like this:
sys.argv[1] == r"(lp0\nS'cat'\np1\naI5\na."
Also, you can use a string in the parameters without quotes.
It is because the syntax of strings in Python and in the shell (presumably Bash) is different.
You may want to run the program as
echo $'"(lp0\nS\'cat\'\np1\naI5\na.'
program.py $'"(lp0\nS\'cat\'\np1\naI5\na.'

Python print in terminal returning 'invalid syntax'

Im using the terminal on my mac to run some python and when i try to print a string i get an invalid syntax error.
Michaels-MBP:~ mike$ python text.py
File "text.py", line 2
print(‘hi’)
^
SyntaxError: invalid syntax
I've tried it with single quotes and with and without parentheses but i keep getting that error, what is wrong.
Should be:
print('hi')
You have proper British quotes ‘foo’. Those are the right symbols to use when writing human-readable texts, but Python wants actual single quotes '.
Your editor probably has some kind of smart-quotes feature enabled, it is wise to turn this off when writing code (e.g. configure your editor to detect extensions like .py).

Passing newline within string into a python script from the command line

I have a script that I run from the command line which I would like to be able to pass string arguments into. As in
script.py --string "thing1\nthing2"
such that the program would interpret the '\n' as a new line. If string="thing1\nthing2" I want to get
print string
to return:
thing1
thing2
rather than thing1\nthing2
If I simply hard-code the string "thing1\nthing2" into the script, it does this, but if it's entered as a command line argument via getopt, it doesn't recognize it. I have tried a number of approaches to this: reading in the cl string as r"%s" % arg, various ways of specifying it on the commandline, etc, and nothing seems to work. Ideas? Is this completely impossible?
From https://stackoverflow.com/a/4918413/478656 in Bash, you can use:
script.py --string $'thing1\nthing2'
e.g.
$ python test.py $'1\n2'
1
2
But that's Bash-specific syntax.
This is really a shell question since the shell does all the command parsing. Python doesn't care what's happening with that and only gets what comes through in the exec system call. If you're using bash, it doesn't do certain kinds of escaping between double quotes. If you want things like \n, \t, or \xnn to be escaped, the following syntax is a bash extension:
python test.py $'thing1\nthing2'
Note that the above example uses single quotes and not double quotes. That's important. Using double quotes causes different rules to apply. You can also do:
python test.py "thing1
thing2"
Here's some more info on bash quoting if you're interested. Even if you're not using bash, it's still good reading:
http://mywiki.wooledge.org/Quotes
This one is relatively simple and I am surprised no one has said it.
In your python script just write the following code
print string.replace("\\n", "\n")
and you will get the string printed with the new line and not the \n.

Unpredictable results from os.path.join in windows

So what I'm trying to do is to join something in the form of
os.path.join('C:\path\to\folder', 'filename').
**edit :
Actual code is :
filename = 'creepy_%s.pcl' % identifier
file = open(os.path.join(self.cache_dir, filename), 'w')
where self.cache_dir is read from a file using configobj (returns string) and in the particular case is '\Documents and Settings\Administrator\creepy\cache'
The first part is returned from a configuration file, using configobj. The second is a concatenation of 2 strings like: 'file%s' % name
When I run the application through the console in windows using the python interpreter installed, I get the expected result which is
C:\\path\\to\\folder\\filename
When I bundle the same application and the python interpreter (same version, 2.6) in an executable in windows and run the app the result is instead
C:\\path\\to\\folderfilename
Any clues as to what might be the problem, or what would cause such inconsistencies in the output ?
Your code is malformed. You need to double those backslashes or use a raw string.
os.path.join('C:\\path\\to\\folder', 'filename').
I don't know why it works in one interpreter and not the other but your code will not be interpreted properly as is. The weird thing is i'd have expected a different output, ie: C:pathtofolder\filename.
It is surprising behavior. There is no reason it should behave in such a way.
Just be be cautious, you can change the line to the following.
os.path.join(r'C:\path\to\folder\', 'filename').
Note the r'' raw string and the final \
Three things you can do:
Use double-slashes in your original string, 'C:\\path\\to\\folder'
Use a raw string, r'C:\path\to\folder'
Use forward-slashes, 'C:/path/to/folder'
I figure it out yesterday. As usual when things seem really strange, the explanation is very simple and most of the times involve you being stupid.
To cut a long story short there were leftovers from some previous installations in dist-packages. The bundled interpreter loaded the module from there , but when i ran the python script from the terminal , the module (newer version) in the current dir was loaded. Hence the "unpredictable" results.

Categories

Resources