I've been adapting an old piece of code to be Python 3 compliant and I came across this individual script
"""Utility functions for processing images for delivery to Tesseract"""
import os
def image_to_scratch(im, scratch_image_name):
"""Saves image in memory to scratch file. .bmp format will be read
correctly by Tesseract"""
im.save(scratch_image_name, dpi=(200, 200))
def retrieve_text(scratch_text_name_root):
inf = file(scratch_text_name_root + '.txt')
text = inf.read()
inf.close()
return text
def perform_cleanup(scratch_image_name, scratch_text_name_root):
"""Clean up temporary files from disk"""
for name in (scratch_image_name, scratch_text_name_root + '.txt',
"tesseract.log"):
try:
os.remove(name)
except OSError:
pass
On the second function, retrieve_text the first line fails with:
Traceback (most recent call last):
File ".\anpr.py", line 15, in <module>
text = image_to_string(Img)
File "C:\Users\berna\Documents\GitHub\Python-ANPR\pytesser.py", line 35, in image_to_string
text = util.retrieve_text(scratch_text_name_root)
File "C:\Users\berna\Documents\GitHub\Python-ANPR\util.py", line 10, in retrieve_text
inf = file(scratch_text_name_root + '.txt')
NameError: name 'file' is not defined
Is this a deprecated function or another problem alltogether? Should I be replacing file() with something like open()?
In Python 2, open and file are mostly equivalent. file is the type and open is a function with a slightly friendlier name; both take the same arguments and do the same thing when called, but calling file to create files is discouraged and trying to do type checks with isinstance(thing, open) doesn't work.
In Python 3, the file implementation in the io module is the default, and the file type in the builtin namespace is gone. open still works, and is what you should use.
You can do what the documentation for file() suggests -
When opening a file, it’s preferable to use open() instead of invoking this constructor directly.
You should use open() method instead.
Related
I'm using Python Version 2.7.10 with macOS Sierra 10.12.16 and Xcode 8.3.3
In the demo program I want to write 2 lines of text in a file.
This should be done in two steps. In the first step the method openNewFile() is called. The file is created with the open command and one line of text is written to the file. The file handle is the return value of the method.
In the second step the method closeNewFile(fH) with the file handle fH as input argument is called. A second line of text should be written to the file and the file should be closed. However, this leads to an error message:
Traceback (most recent call last):
File "playground.py", line 23, in <module>
myDemo.createFile()
File "playground.py", line 20, in createFile
self.closeNewFile(fH)
File "playground.py", line 15, in closeNewFile
fileHandle.writelines("Second line")
ValueError: I/O operation on closed file
Program ended with exit code: 1
It seems to me that handling the file over from one method to another could be the problem.
#!/usr/bin/env python
import os
class demo:
def openNewFile(self):
currentPath = os.getcwd()
myDemoFile = os.path.join(currentPath, "DemoFile.txt")
with open(myDemoFile, "w") as f:
f.writelines("First line")
return f
def closeNewFile(self, fileHandle):
fileHandle.writelines("Second line")
fileHandle.close()
def createFile(self):
fH = self.openNewFile()
self.closeNewFile(fH)
myDemo = demo()
myDemo.createFile()
What are I doing wrong?
How can this problem be fixed?
You're mistaken about what with....as does. This code is the culprit here:
with open(myDemoFile, "w") as f:
f.writelines("First line")
return f
Just before the return, with closes the file, so you end up returning a closed file from the function.
I should add -- opening a file in one function and returning it without closing it (what your actual intention is) is major code smell. That said, the fix to this problem would be to get rid of the with...as context manager:
f = open(myDemoFile, "w")
f.writelines("First line")
return f
An improvement to this would be to not get rid of your context manager, but to carry out all your I/O within the context manager. Don't have separate functions for opening and writing, and don't segment your I/O operations.
I am using Python pdftables to fetch table data from pdf and i followed the instructions as give in git
https://github.com/drj11/pdftables
but when i run the code
filepath = 'tests.pdf'
fileobj = open(filepath,'rb')
from pdftables.pdf_document import PDFDocument
doc = PDFDocument.from_fileobj(fileobj)
i get error like this
File "<stdin>", line 1, in <module>
File "pdftables/pdf_document.py", line 53, in from_fileobj
raise NotImplementedError
can any anyone help me out in this problem
If you look at the file implementing the from_fileobj function you can see the following comment:
# TODO(pwaller): For now, put fh into a temporary file and call
# .from_path. Future: when we have a working stream input function for
# poppler, use that.
If I understand it correctly you should instead use the from_path function as from_fileobj is not implemented yet. This is easy with your current code:
filepath = 'tests.pdf'
from pdftables.pdf_document import PDFDocument
doc = PDFDocument.from_path(filepath)
I am trying to pass an argument from batch file to my python file.
I followed the steps given in these two links:
Passing Argument from Batch File to Python
Sending arguments from Batch file to Python script
Here is a part of my python file where I'm trying to pass argument:
def main(argv):
imapServ = 'imap.gmail.com'
filename = 'TestRunLog.log'
attachment = open("{} {}".format(argv[0], filename), 'rb')
....##rest of the code
import sys
try:
if __name__ == '__main__':
print 'go ahead'
main(sys.argv[:1])
except ImportError:
print 'hi'
Also, here is the part of batch file which I'm using to send argument to the Python file:
c:\python27\python.exe C:\Users\abcd\Documents\automation\testsendemail.py %%myhome%\Documents\automation\Testresults\%resultDir%
pause
Above, %resultDir% is the variable which is generated based on timestamp.
Here is the output:
go ahead
Traceback (most recent call last):
C:/Users/abcd/Documents/automation/testsendemail.py\TestRunLog.log
File "C:/Users/abcd/Documents/automation/testsendemail.py", line 44, in <module>
main(sys.argv[:1])
File "C:/Users/abcd/Documents/automation/testsendemail.py", line 25, in main
attachment = open("{} {}".format(argv[0], filename), 'rb')
IOError: [Errno 2] No such file or directory: 'C:/Users/abcd/Documents/automation/testsendemail.py TestRunLog.log'
I followed lots of stackoverflow questions regarding this issue but still I'm unable to run. Not sure where the mistake is.
The issue is related on how python works with argv.
In this scenario, when you run:
main(sys.argv[:1]) # (["C:\Users\abcd\Documents\automation\testsendemail.py"])
you actually get only the first arguments passed to the python script, which is the current script location.
To get all the arguments but the first, you must fix that array filter:
main(sys.argv[1:]) # ["%%myhome%\Documents\automation\Testresults\%resultDir%"])
Note that the second filter will also include any other arguments that you might add to the command line.
Also, as a side note. You should consider using the STD lib to join the paths.
It should be something like this:
from os.path import join
(...)
filename = 'TestRunLog.log'
attachment = open(join(argv[0], filename), 'rb')
Is it possible to use the maya.cmds instead of using any maya API to load/import in a file format in which it is not part of Maya file types?
I have tried googling but to no avail results other than the fileDialog command in Maya, otherwise it would means I will need to implement maya API (where I totally do not have any experiences with it)
I tried the following:
multipleFilters = "chan (*.chan)"
fileList = cmds.fileDialog2(fileMode=1, fileFilter=multipleFilters, dialogStyle=2)
if not fileList:
# return or print something or bail out early
filename = fileList[0]
cmds.file(filename, i=True)
Instead I keep getting the following error:
# Error: Unrecognized file.
# Traceback (most recent call last):
# File "<maya console>", line 3, in <module>
# RuntimeError: Unrecognized file. #
Any ideas?
cmds.file only works for files with translators that are registered via the API, either in Python or C++.
You can, however, easily write python (or even mel) scripts which read files off disk and create stuff in your scenes. You can use cmds.fileDiialog2 to present a file dialog to the user to pick file off disk, but it will be up to you to read the file.
multipleFilters = "chan (*.chan)"
fileList = cmds.fileDialog2(fileMode=1, fileFilter=multipleFilters, dialogStyle=2)
with open (fileList[0], 'rt') as filehandle:
for line in filehandle:
print line # or do something useful with the data
tempfile.mkstemp() returns:
a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.
How do I convert that OS-level handle to a file object?
The documentation for os.open() states:
To wrap a file descriptor in a "file
object", use fdopen().
So I tried:
>>> import tempfile
>>> tup = tempfile.mkstemp()
>>> import os
>>> f = os.fdopen(tup[0])
>>> f.write('foo\n')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor
You can use
os.write(tup[0], "foo\n")
to write to the handle.
If you want to open the handle for writing you need to add the "w" mode
f = os.fdopen(tup[0], "w")
f.write("foo")
Here's how to do it using a with statement:
from __future__ import with_statement
from contextlib import closing
fd, filepath = tempfile.mkstemp()
with closing(os.fdopen(fd, 'w')) as tf:
tf.write('foo\n')
You forgot to specify the open mode ('w') in fdopen(). The default is 'r', causing the write() call to fail.
I think mkstemp() creates the file for reading only. Calling fdopen with 'w' probably reopens it for writing (you can reopen the file created by mkstemp).
temp = tempfile.NamedTemporaryFile(delete=False)
temp.file.write('foo\n')
temp.close()
What's your goal, here? Is tempfile.TemporaryFile inappropriate for your purposes?
I can't comment on the answers, so I will post my comment here:
To create a temporary file for write access you can use tempfile.mkstemp and specify "w" as the last parameter, like:
f = tempfile.mkstemp("", "", "", "w") # first three params are 'suffix, 'prefix', 'dir'...
os.write(f[0], "write something")