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.
Related
After looking for a large amount of time, I still can't seem to find an answer to my problem (I am new to python).
Here is what I'm trying to do :
Prompt the user to insert an nlps version and ncs version (both are just server builds)
Get all the filenames ending in .properties in a specified folder
Read those files to find the old nlps and ncs version
Replace, in the same .properties files, the old nlps and ncs versions by the ones given by the user
Here is my code so far :
import glob, os
import fileinput
nlpsversion = str(input("NLPS Version : "))
ncsversion = str(input("NCS Version : "))
directory = "C:/Users/x/Documents/Python_Test"
def getfilenames():
filenames = []
os.chdir(directory)
for file in glob.glob("*.properties"):
filenames.append(file)
return filenames
properties_files = getfilenames()
def replaceversions():
nlpskeyword = "NlpsVersion"
ncskeyword = "NcsVersion"
for i in properties_files:
searchfile = open(i, "r")
for line in searchfile:
if line.startswith(nlpskeyword):
old_nlpsversion = str(line.split("=")[1])
if line.startswith(ncskeyword):
old_ncsversion = str(line.split("=")[1])
for line in fileinput.FileInput(i,inplace=1):
print(line.replace(old_nlpsversion, nlpsVersion))
replaceversions()
In the .properties files, the versions would be written like :
NlpsVersion=6.3.107.3
NcsVersion=6.4.000.29
I am able to get old_nlpsversion and old_ncsversion to be 6.3.107.3 and 6.4.000.29. The problem occurs when I try to replace the old versions with the ones the user inputed. I get the following error :
C:\Users\X\Documents\Python_Test>python replace.py
NLPS Version : 15
NCS Version : 16
Traceback (most recent call last):
File "replace.py", line 43, in <module>
replaceversions()
File "replace.py", line 35, in replaceversions
for line in fileinput.FileInput(i,inplace=1):
File "C:\Users\X\AppData\Local\Programs\Python\Python36-
32\lib\fileinput.py", line 250, in __next__
line = self._readline()
File "C:\Users\X\AppData\Local\Programs\Python\Python36-
32\lib\fileinput.py", line 337, in _readline
os.rename(self._filename, self._backupfilename)
PermissionError: [WinError 32] The process cannot access the file because it
is being used by another process: 'test.properties' -> 'test.properties.bak'
It may be that my own process is the one using the file, but I can't figure out how to replace, in the same file, the versions without error. I've tried figuring it out myself, there are a lot of threads/resources out there on replacing strings in files, and i tried all of them but none of them really worked for me (as I said, I'm new to Python so excuse my lack of knowledge).
Any suggestions/help is very welcome,
You are not releasing the file. You open it readonly and then attempt to write to it while it is still open. A better construct is to use the with statement. And you are playing fast and loose with your variable scope. Also watch your case with variable names. Fileinput maybe a bit of overkill for what you are trying to do.
import glob, os
import fileinput
def getfilenames(directory):
filenames = []
os.chdir(directory)
for file in glob.glob("*.properties"):
filenames.append(file)
return filenames
def replaceversions(properties_files,nlpsversion,ncsversion):
nlpskeyword = "NlpsVersion"
ncskeyword = "NcsVersion"
for i in properties_files:
with open(i, "r") as searchfile:
lines = []
for line in searchfile: #read everyline
if line.startswith(nlpskeyword): #update the nlpsversion
old_nlpsversion = str(line.split("=")[1].strip())
line = line.replace(old_nlpsversion, nlpsversion)
if line.startswith(ncskeyword): #update the ncsversion
old_ncsversion = str(line.split("=")[1].strip())
line = line.replace(old_ncsversion, ncsversion)
lines.append(line) #store changed and unchanged lines
#At the end of the with loop, python closes the file
#Now write the modified information back to the file.
with open(i, "w") as outfile: #file opened for writing
for line in lines:
outfile.write(line+"\n")
#At the end of the with loop, python closes the file
if __name__ == '__main__':
nlpsversion = str(input("NLPS Version : "))
ncsversion = str(input("NCS Version : "))
directory = "C:/Users/x/Documents/Python_Test"
properties_files = getfilenames(directory)
replaceversions(properties_files,nlpsversion,ncsversion)
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')
I have the following code, and am getting an error: I/O operation on a closed file despite having opened the file.
I am creating a .txt file and writing values of a dictionary to the .txt file, then closing the file.
After that I am trying to print the SHA256 digest for the file created.
sys.stdout = open('answers.txt', 'w')
for key in dictionary:
print(dictionary[key])
sys.stdout.close()
f = open('answers.txt', 'r+')
#print(hashlib.sha256(f.encode('utf-8')).hexdigest())
m = hashlib.sha256()
m.update(f.read().encode('utf-8'))
print(m.hexdigest())
f.close()
Why am I getting this error?
Traceback (most recent call last):
File "filefinder.py", line 97, in <module>
main()
File "filefinder.py", line 92, in main
print(m.hexdigest())
ValueError: I/O operation on closed file.
Here, you override sys.stdout to point to your opened file:
sys.stdout = open('answers.txt', 'w')
Later, when you try to print to STDOUT sys.stdout is still pointing to the (now closed) answers.txt file:
print(m.hexdigest())
I don't see any reason to override sys.stdout here. Instead, just pass a file option to print():
answers = open('answers.txt', 'w')
for key in dictionary:
print(dictionary[key], file=answers)
answers.close()
Or, using the with syntax that automatically closes the file:
with open('answers.txt', 'w') as answers:
for key in dictionary:
print(dictionary[key], file=answers)
You have been overwriting sys.stdout with a file handle. As soon as you close it, you can write to it anymore. Since print() tries to write to sys.stdout it will fail.
You should try opening the file in a different mode (w+ for example), use a StringIO or copy the original sys.stdout and restore it later.
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.
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