Python code breaks in python3.
Vectorize.py contains the following line
path = os.path.join('..', path[:-1])
Error output:
$ python3 vectorize_text.py
Traceback (most recent call last):
File "vectorize_text.py", line 46, in <module>
path = os.path.join('..', path[:-1])
File "/usr/lib/python3.4/posixpath.py", line 89, in join
"components") from None
TypeError: Can't mix strings and bytes in path component
On the other hand, running in python2.7 it works fine. What am I missing here ? Is the command different ? I couldn't find anything.
Your path is a bytes object, not a str string. You can then only use more bytes strings to make a different path. Use a b'..' bytes literal:
path = os.path.join(b'..', path[:-1])
Related
We have images in our OpenStack named <OS> <version>:<build no> (e.g., CentOS 7.2.0:160708.0). With the Python novaclient, I can use client.glance.find_image with releases before Mitaka.
$ cat test.py
#! /usr/bin/env python3
import os
import sys
from novaclient import client
nova = client.Client("2",
os.environ["OS_USERNAME"],
os.environ["OS_PASSWORD"],
os.environ["OS_TENANT_ID"],
os.environ["OS_AUTH_URL"],
cacert=os.environ["OS_CACERT"])
print(nova.glance.find_image(sys.argv[1]))
With Liberty:
$ python3 test.py "CentOS 7.2.0:170210.0"
<Image: CentOS 7.2.0:170210.0>
With Mitaka:
$ python3 test.py "CentOS 7.2.0:170210.0"
Traceback (most recent call last):
File "test.py", line 11, in <module>
print(nova.glance.find_image(sys.argv[1]))
File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 53, in find_image
"images")
File "/usr/local/lib/python3.6/site-packages/novaclient/base.py", line 254, in _list
resp, body = self.api.client.get(url)
File "/usr/local/lib/python3.6/site-packages/keystoneauth1/adapter.py", line 223, in get
return self.request(url, 'GET', **kwargs)
File "/usr/local/lib/python3.6/site-packages/novaclient/client.py", line 80, in request
raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.BadRequest: Unable to filter by unknown operator 'CentOS 7.2.0'.<br /><br />
(HTTP 400)
Note that the error when an image of that name does not exist is different:
$ python3 test.py "CentOS 7.2.0"
Traceback (most recent call last):
File "test.py", line 11, in <module>
print(nova.glance.find_image(sys.argv[1]))
File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 58, in find_image
raise exceptions.NotFound(404, msg)
novaclient.exceptions.NotFound: No Image matching CentOS 7.2.0. (HTTP 404)
It's as if find_image is expecting a string of the form operator: value, but the documentation has only this to say about find_image:
find_image(name_or_id)
Find an image by name or id (user provided input).
How do I find an image whose name contains a colon when using Mitaka?
$ nova --version
8.0.0
The error is coming from the image service (Glance). In the newer versions of Glance, there is a change in the GET API syntax, where someone can specify an "in:" operator for filtering. You can read more about this at
https://developer.openstack.org/api-ref/image/v2/index.html?expanded=show-images-detail#show-images
For your code to work, you can enclose the image name with quotes and prefix it with "in:" string:
print(nova.glance.find_image('in:"' + sys.argv[1] + '"'))
Note that Glance is pretty strict about quotes; your image name has to be wrapped with double-quotes only -- single quotes won't work. Hence, I used the single quote for the string in the above command.
Another pretty inefficient but functional option is to use list() function in nova.images and then explicitly look for the image with name sys.argv[1]:
ilist = nova.images.list()
for image in ilist:
if image.name == sys.argv[1]:
print image
break
I have a file encoded in hex and I'm trying to decode the file however I keep getting a type error. I have only been using python on and off for a few weeks so if this seems like a basic question I apologize.
The file contents is as follows:
4647525137454353554e54544b5831375a42524d345742473246563639554e4a36495a3359304f35394843554637564d4d464f32354143574f495a4f4a4a565849373259544f46335a4358494b424e335047545a51534b47465259475956584d44594f473536494553373653455932574b33574431435a314d35545957594d4e57434444344948324d375858544f4c564f31444a45304947394c32375a584f4845535a534f43353859594c55594e4239363759393738313557475859345a474448434e4f5a5744544d696c6c656e69756d323030303a3035303233626566343737386639343461626439346334653364623062326166
here is the code I ran:
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
This is what I got back:
Traceback (most recent call last):
File "converter.py", line 1, in <module>
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found
You're giving it a filename rather than the contents of that file:
"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
Try this:
open("received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt").read().decode("hex")
I am using SOAPpy for soap wsdl services. I am following this toturail. My code is as follow
from SOAPpy import WSDL
wsdlfile = 'http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL'
server = WSDL.Proxy(wsdlfile)
I am getting this error on the last line of my code
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/SOAPpy/WSDL.py", line 85, in __init__
self.wsdl = reader.loadFromString(str(wsdlsource))
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/wstools/WSDLTools.py", line 52, in loadFromString
return self.loadFromStream(StringIO(data))
TypeError: initial_value must be unicode or None, not str
I tried to convert the string into utf using
wsdlFile = unicode('http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL, "utf-8")
but still having same error. What is missing here ?
I just ran into this problem with some very old 2.7 code that no longer worked due to the TLS update. After updating to the most recent version of Python 2 I ended up getting this issue.
I was only able to fix this by setting up a new virtual environment, then modifying the wstools package in that virtual environment to use BytesIO instead of StringIO.
Replace every required instance of StringIO. For example:
# WSDLTools.py
...
from IO import BytesIO
...
return self.loadFromStream(BytesIO(data))
Not ideal, but it worked. Easier than migrating everything to Python 3...
I've got PyPDF2 lib from here:
https://github.com/mstamy2/PyPDF2/tree/Python3-3
When trying to run script "Example 1:" from from there see it:
PyPDF2 python versions (2.5 - 3.3) compatibility branch
Traceback (most recent call last):
File "1.py", line 6, in <module>
input1 = PdfFileReader(open("document1.pdf", "rb"))
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 595, in __init__
self.read(stream)
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 1097, in read
streamData = StringIO(xrefstream.getData())
TypeError: initial_value must be str or None, not bytes
What is wrong?
It was a problem related to the compatibility within PyPDF2 and Python 3.
In my case, I have solved it by replacing pdf.py and utils.py with the ones you will find here, where they basically control if you are running Python 3 and, in case you are, receive data as bytes instead of strings.
I know this may seem rude or mean or unpolite, but I need some help to try to figure out why I cant call window.loadPvmFile("f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm") exactly like that as a string. Instead of doing that, it gives me a traceback error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 415, in <module>
window.loadPvmFile("f:\games\#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")
File "F:\Python Apps\pvmViewer_v1_1.py", line 392, in loadPvmFile
file1 = open(path, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename:
'f:\\games\\#DD.ATC3.Root\\common\\models\x07300\x07mu\\dummy.pvm'
Also notice, that in the traceback error, the file path is different. When I try a path that has no letters in it except for the drive letter and filename, it throws this error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 416, in <module>
loadPvmFile('f:\0\0\dummy.pvm')
File "F:\Python Apps\pvmViewer_v1_1.py", line 393, in loadPvmFile
file1 = open(path, "r")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
I have searched for the place that the encode function is called or where the argument is encoded and cant find it. Flat out, I am out of ideas, frustrated and I have nowhere else to go. The source code can be found here: PVM VIEWER
Also note that you will not be able to run this code and load a pvm file and that I am using portable python 2.7.3! Thanks for everyone's time and effort!
\a and \0 are escape sequences. Use r'' (or R'') around the string to mark it as a raw string.
window.loadPvmFile(r"f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")