OSError: cannot open resources error while running .exe file - python

I just wrote the barcode generator code. The work of the program is to generate the code. I am taking data through the XML file then I will generate the barcode. Everything is working fine in .py file but when I convert the .py file to .exe file then it will not generate the barcode then shows OSError: cannot open resources.
My code is
from barcode.writer import ImageWriter
import xml.dom.minidom
DOMTree = xml.dom.minidom.parse('codedata.xml')
ENVELOPE = DOMTree.documentElement
QRCODE = ENVELOPE.getElementsByTagName("QRCODE")[0]
INVOICE = QRCODE.getElementsByTagName('INVOICE')[0]
dataget=INVOICE.childNodes[0].data
with open('barcode_image.jpeg', 'wb') as f:
barcode.Code128(str(dataget),writer=ImageWriter()).write(f) # error shows in this line
I don't know what to do please help me.

Related

Python code working on terminal but not on web server

I'm executing these lines of code on python terminal and it is working but when i execute it on webserver which is xampp i get output 255 in transcribe_text.txt file but when i run through terminal i get audio to text proper conversion. I'm working on windows environment.
import subprocess
out=subprocess.getstatusoutput(['deepspeech', '--model', 'C:/deepspeech/deepspeech-0.6.0-models/output_graph.pb', '--lm', 'C:/deepspeech/deepspeech-0.6.0-models/lm.binary', '--trie', 'C:/deepspeech/deepspeech-0.6.0-models/trie', '--audio', 'C:/deepspeech/audio/8455-210777-0068.wav'])
#open and read the file after the appending:
print(str(out))
f = open("transcribe_text.txt", "a")
f.write(str(out))
f.close()
#open and read the file after the appending:
f = open("transcribe_text.txt", "r")
print(f.read())
Any solution Please, Thanks

could not find file existing file while converting from .tex to pdf in python

I need to convert latex file to pdf. I try to do it like in documentation written:
import subprocess, os
from tex import latex2pdf,tex2pdf
from latex import build_pdf
from pdflatex import PDFLaTeX
with open('sometexfile.tex','w') as file:
file.write('\\documentclass{article}\n')
file.write('\\begin{document}\n')
file.write('Hello Palo Alto!\n')
file.write('\\end{document}\n')
with open('sometexfile.tex', 'rb') as f:
file_to_read = f.read()
pdfl = PDFLaTeX.from_binarystring(file_to_read , 'sometexfile')
pdf = pdfl.create_pdf()
And I got an error:
line 20, in
pdf = pdfl.create_pdf() The system cannot find the file specified
How should I solve my problem?

Creating view in browser functionality with python

I have been struggling with this problem for a while but can't seem to find a solution for it. The situation is that I need to open a file in browser and after the user closes the file the file is removed from their machine. All I have is the binary data for that file. If it matters, the binary data comes from Google Storage using the download_as_string method.
After doing some research I found that the tempfile module would suit my needs, but I can't get the tempfile to open in browser because the file only exists in memory and not on the disk. Any suggestions on how to solve this?
This is my code so far:
import tempfile
import webbrowser
# grabbing binary data earlier on
temp = tempfile.NamedTemporaryFile()
temp.name = "example.pdf"
temp.write(binary_data_obj)
temp.close()
webbrowser.open('file://' + os.path.realpath(temp.name))
When this is run, my computer gives me an error that says that the file cannot be opened since it is empty. I am on a Mac and am using Chrome if that is relevant.
You could try using a temporary directory instead:
import os
import tempfile
import webbrowser
# I used an existing pdf I had laying around as sample data
with open('c.pdf', 'rb') as fh:
data = fh.read()
# Gives a temporary directory you have write permissions to.
# The directory and files within will be deleted when the with context exits.
with tempfile.TemporaryDirectory() as temp_dir:
temp_file_path = os.path.join(temp_dir, 'example.pdf')
# write a normal file within the temp directory
with open(temp_file_path, 'wb+') as fh:
fh.write(data)
webbrowser.open('file://' + temp_file_path)
This worked for me on Mac OS.

Error while opening external text file in python

Code and Error messageI am unable to open text file in python despite of writing correct code. Screenshot of code, error and file location is as followsText file location
You should enter adnan.txt as input, since it's a text file.
You have to change the lines to:
fhand = open(raw_input("Enter a filename: "),'r')
And then the rest of the code
edit
As John Smith says, be careful with the name you are trying to use, in your case seems to be adnan.txt
I attach here a piece of code that could work for you:
import os
MY_FILENAME = "adnan.txt" # don't forget the extension
with open(os.path.join(os.getcwd(),'MY_FILENAME'), 'r') as my_file:
f = myfile.readlines()
for line in f:
print(line)
I used os.getcwd() as your file seems to be in the script directory

Python: Crossplatform code to download a valid .zip file

I have a requirement to download and unzip a file from a website. Here is the code I'm using:
#!/usr/bin/python
#geoipFolder = r'/my/folder/path/ ' #Mac/Linux folder path
geoipFolder = r'D:\my\folder\path\ ' #Windows folder path
geoipFolder = geoipFolder[:-1] #workaround for Windows escaping trailing quote
geoipName = 'GeoIPCountryWhois'
geoipURL = 'http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip'
import urllib2
response = urllib2.urlopen(geoipURL)
f = open('%s.zip' % (geoipFolder+geoipName),"w")
f.write(repr(response.read()))
f.close()
import zipfile
zip = zipfile.ZipFile(r'%s.zip' % (geoipFolder+geoipName))
zip.extractall(r'%s' % geoipFolder)
This code works on Mac and Linux boxes, but not on Windows. There, the .zip file is written, but the script throws this error:
zipfile.BadZipfile: File is not a zip file
I can't unzip the file using Windows Explorer either. It says that:
The compressed (zipped) folder is empty.
However the file on disk is 6MB large.
Thoughts on what I'm doing wrong on Windows?
Thanks
Your zipfile is corrupt on windows because you're opening the file in write/text mode (line-terminator conversion trashes binary data):
f = open('%s.zip' % (geoipFolder+geoipName),"w")
You have to open in write/binary mode like this:
f = open('%s.zip' % (geoipFolder+geoipName),"wb")
(will still work on Linux of course)
To sum it up, a more pythonic way of doing it, using a with block (and remove repr):
with open('{}{}.zip'.format(geoipFolder,geoipName),"wb") as f:
f.write(response.read())
EDIT: no need to write a file to disk, you can use io.BytesIO, since the ZipFile object accepts a file handle as first parameter.
import io
import zipfile
with open('{}{}.zip'.format(geoipFolder,geoipName),"wb") as f:
outbuf = io.BytesIO(f.read())
zip = zipfile.ZipFile(outbuf) # pass the fake-file handle: no disk write, no temp file
zip.extractall(r'%s' % geoipFolder)

Categories

Resources