"cannot execute binary file" error in python - python

I keep getting the following error:
$ ./test.py
-bash: ./test.py: cannot execute binary file
when trying to run the following file in python via cygwin:
#!usr/bin/python
with open("input.txt") as inf:
try:
while True:
latin = inf.next().strip()
gloss = inf.next().strip()
trans = inf.next().strip()
process(latin, gloss, trans)
inf.next() # skip blank line
except StopIteration:
# reached end of file
pass
from itertools import chain
def chunk(s):
"""Split a string on whitespace or hyphens"""
return chain(*(c.split("-") for c in s.split()))
def process(latin, gloss, trans):
chunks = zip(chunk(latin), chunk(gloss))
How do I fix this??
After taking on the below suggestions, still getting the same error.
If this helps, I tried
$ python ./test.py
and got
$ python ./test.py
File "./test.py", line 1
SyntaxError: Non-ASCII character '\xff' in file ./test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

There is a problem. You are missing the '/' in front of usr in #!usr/bin/python. Your line should look like this.
#!/usr/bin/python

In addition to protecting the file executable, #!/usr/bin/python may not work. At least it has never worked for me on Red Hat or Ubuntu Linux. Instead, I have put this in my Python files:
#!/usr/bin/env python
I don't know how this works on Windows platforms.

Related

Python3: File <stdin> TypeError: 'str' does not support the buffer interface

I know similar problems have been asked many times, but I don't think they solved my mine.
I'm basically trying to convert the following bash command into python using the subprocess module:
cat <a python file> | ssh <ip> python - <args>
Here's what I have:
with open('<the python file>', 'r') as python_file:
print(subprocess.check_output(['ssh', '<ip>', 'python - <args>', ], stdin=python_file))
And I'm running the above code from my local laptop using python3. When on the target machine (the remote server with <ip>) the python version is 2.7, this code snippet works fine. But if I switch the python version of the target machine to 3.4, I got the error:
File "<stdin>", line 114, in <module>
File "<stdin>", line 94, in main
File "<stdin>", line 22, in load
TypeError: 'str' does not support the buffer interface
I've tried the following:
open the python file with in binary mode: open('<a python file'>, 'rb')
change the subprocess args from ['ssh', '<ip>', 'python - <args>'] to ['ssh', '<ip>', 'python', '-', '<args>'].
But none of them worked. Can anyone give some suggestions? Thanks!
[Update]
Thanks to #Justin Ezequiel 's comment, this turned out to be a problem of '<the python file>' instead of the code snippet above. In '<the python file>' there is the following code:
columns = ['KNAME', 'TYPE', 'SIZE', 'MOUNTPOINT'] # (1)
lsblk_args = ['lsblk', '-i', '-n', '-o', ','.join(columns)] # (2)
output = subprocess.check_output(lsblk_args).strip() # (3)
parsed_res = _parse(output.split('\n'), columns) # (4)
And if I change the (3) line to
output = subprocess.check_output(lsblk_args).decode('utf-8').strip()
Everything works just fine.

Script working on Windows, but not on Linux?

I am learning to program in Python and am still at the very beginning.
I wrote a 2 scripts to cut out IP-addresses from a nmap-output.
The first script cuts out the IP-addresses:
import re
file = open("test.txt", "r")
ips = open("ips.txt", "w+")
for line in file:
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
if "filtered" in line:
ips.write(str(ip) + "\n")
This code works fine on Windows and Linux, but (I hope I'm right) the for-loop gets every line as a list. That means, my IP-addresses have the format ['x.x.x.x'].
I wrote a second script to delete all the unnecessary characters ([ ' and ]):
ip = open("ips.txt", "r")
ip_filtered = open("ip_filtered.txt", "w")
for line in ip:
s = line
neu = s.translate(({ord(i): None for i in '[\']'}))
ip_filtered.write(str(neu))
This script works well on Windows (I got a new file, just with IP-addresses), but on Linux I get the following error:
Traceback (most recent call last):
File "zeichen_loeschen.py", line 6, in <module>
neu = s.translate(({ord(i): None for i in '[\']'}))
TypeError: expected a string or other character buffer object
What's the reason for this error?
Thanks in advance :)
I get the same message when running with the python command on linux (which uses python 2.7). Try running it with the python3 command and your code should work.

Unable to read dicom file with Python3 and pydicom

I trying to read dicom file with python3 and pydicom library. For some dicom data, I can't get data correctly and get error messages when I tried to print the result of pydicom.dcmread.
However, I have tried to use python2 and it worked well. I checked out the meta information and compared it with other dicom files which can be processed, I didn't find any difference between them.
import pydiom
ds = pydicom.dcmread("xxxxx.dicom")
print(ds)
Traceback (most recent call last):
File "generate_train_data.py", line 387, in <module>
tf.app.run()
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "generate_train_data.py", line 371, in main
create_ann()
File "generate_train_data.py", line 368, in create_ann
ds_ann_dir, case_name, merge_channel=False)
File "generate_train_data.py", line 290, in process_dcm_set
all_dcms, dcm_truth_infos = convert_dicoms(dcm_list, zs)
File "generate_train_data.py", line 179, in convert_dicoms
instance_num, pixel_spacing, img_np = extract_info(dcm_path)
File "generate_train_data.py", line 147, in extract_info
print(ds)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 2277-2279: ordinal not in range(128)
Anyone has come across the same problem?
Can you give an example for such a dicom file? When running the pydicom example with python 3.7 it's working perfectly:
import matplotlib.pyplot as plt
import pydicom
from pydicom.data import get_testdata_files
filename = get_testdata_files("CT_small.dcm")[0]
ds = pydicom.dcmread(filename)
plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
It's also working with the sample dicom files from the Medical Image Samples.
I believe the cause of the problem is that Python (for me it only happened in Python 3 running on Centos 7.6 Linux printing to a terminal window on MacOS) is not able to figure out how to print a string that contains a non-ascii character because of the setting of the locale. You can use the locale command to see the results. Mine started out with everything set to "C". I set the LANG environment variable to en_US.UTF-8. With that setting it worked for me.
In csh this is done using
setenv LANG en_US.UTF-8
In bash use:
export LANG=en_US.UTF-8
My problem resulted from having 'ยต' in the Series Description element. The file was an attenuation map from a SPECT reconstruction on a Siemens scanner. I used the following Python code to help figure out the problem.
#! /usr/bin/env python3
import pydicom as dicom
from sys import exit, argv
def myprint(ds, indent=0):
"""Go through all items in the dataset and print them with custom format
Modelled after Dataset._pretty_str()
"""
dont_print = ['Pixel Data', 'File Meta Information Version']
indent_string = " " * indent
next_indent_string = " " * (indent + 1)
for data_element in ds:
if data_element.VR == "SQ": # a sequence
print(indent_string, data_element.name)
for sequence_item in data_element.value:
myprint(sequence_item, indent + 1)
print(next_indent_string + "---------")
else:
if data_element.name in dont_print:
print("""<item not printed -- in the "don't print" list>""")
else:
repr_value = repr(data_element.value)
if len(repr_value) > 50:
repr_value = repr_value[:50] + "..."
try:
print("{0:s} {1:s} = {2:s}".format(indent_string,
data_element.name,
repr_value))
except:
print(data_element.name,'****Error printing value')
for f in argv[1:]:
ds = dicom.dcmread(f)
myprint(ds, indent=1)
This is based on the myprint function from]1
The code tries to print out all the data items. It catches exceptions and prints "****Error printing value" when there is an error.

Pycharm cannot recognize print option args

I am using PyCharm CE 2016.2 and python interpreter 3.5. When I tried to type the following in console, the file is generated with '123'.
>>> with open('man_data.txt', 'w') as data:
print('123', file=data)
However, when I put the same statement into a test.py file and try to run it using subprocess.call(['python', 'test.py']), it gives
>>> subprocess.call(['python', 'test.py'])
File "test.py", line 2
print('123', file=data)
^
SyntaxError: invalid syntax
1
Does anyone has any clues? Million thanks!
Python 2 is started when you call subprocess.call(['python', 'test.py']).
Try to change to subprocess.call(['python3', 'test.py']) or update the script as described here.

Python cPickle unable to load an OCR model library

I have just installed ocropus OCR with all dependencies in my windows 7 machine. (I am using 32bit python 2.7) It seems to be working fine except that I cannot load the default OCR model: en-default.pyrnn.gz. , and receiving a Traceback. I am using the following syntax:
python ocropus-rpred -m en-default.pyrnn.gz book\0001\*.png
here is the error
INFO: #inputs47
# loading object /usr/local/share/ocropus/en-default.pyrnn.gz
Traceback (most recent call last):
File "ocropus-rpred" line 109, in <module>
network = ocrolib.load_object(args.model,verbose=1)
File "C:\anaconda32\lib\site-packages\ocrolib\common.py", line 513, in load_object
return unpickler.load()
EOFError
I have checked the file is not empty; also double checked the binary mode flag enabled i.e. "wb" and "rb"; also converted the newlines of common.py using dos2unix. I am being unable to unable to solve this problem. If anyone have expereinced similar issues, kindly share.
import cPickle
import gzip
def save_object(fname,obj,zip=0):
if zip==0 and fname.endswith(".gz"):
zip = 1
if zip>0:
# with gzip.GzipFile(fname,"wb") as stream:
with os.popen("gzip -9 > '%s'"%fname,"wb") as stream:
cPickle.dump(obj,stream,2)
else:
with open(fname,"wb") as stream:
cPickle.dump(obj,stream,2)
def unpickle_find_global(mname,cname):
if mname=="lstm.lstm":
return getattr(lstm,cname)
if not mname in sys.modules.keys():
exec "import "+mname
return getattr(sys.modules[mname],cname)
def load_object(fname,zip=0,nofind=0,verbose=0):
"""Loads an object from disk. By default, this handles zipped files
and searches in the usual places for OCRopus. It also handles some
class names that have changed."""
if not nofind:
fname = ocropus_find_file(fname)
if verbose:
print "# loading object",fname
if zip==0 and fname.endswith(".gz"):
zip = 1
if zip>0:
# with gzip.GzipFile(fname,"rb") as stream:
with os.popen("gunzip < '%s'"%fname,"rb") as stream:
unpickler = cPickle.Unpickler(stream)
unpickler.find_global = unpickle_find_global
return unpickler.load()
else:
with open(fname,"rb") as stream:
unpickler = cPickle.Unpickler(stream)
unpickler.find_global = unpickle_find_global
return unpickler.load()
UPDATE: Hi, please note that I have used Python's native gzip, and it is working fine. Thank you for pointing that out. Here is the correct syntax that is working on Windows: {with gzip.GzipFile(fname,"rb") as stream:}
Your use of gunzip (in the load_object function) is incorrect. Unless passed the -c argument, gunzip writes the decompressed data to a new file, not to its stdout (which is what you seem to be attempting to do).
As a result, it doesn't write anything to its stdout, and your stream variable contains no data, hence the EOFError.
A quick fix is to change your gunzip command line to give it the -c argument.
More info here: http://linux.die.net/man/1/gzip
That said, why are you even shelling out to gunzip to decompress your data? Python's built-in gzip module should handle that without problems.

Categories