Unable to open a previously opened H5 - python

I have a simple python script that opens an H5 file, edits some of the data and closes it. For some reason it works the first time I run the script but in crashes on the second try.
I was expecting that the error was that I do not close the file but actually I do. As you can see below I edit the fields called backR frontR and I create two new ones Manufacturer and Status then I close.
f = h5py.File(filename, 'r+')
backR = f['back_R']
backR[...] = SelectedBackCoat
frontR = f['front_R']
frontR[...] = SelectedFrontCoat
f.create_dataset('manufacturer', data=SelectedManu)
f.create_dataset('status', data=SelectedState)
f.close()
The second time I run the script for the same file to treat I get the following:
File "h5py\h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (file is already open for read-only)

The file is still opened, maybe your script didn't reach f.close()? From this answer to this question, you can try to force close all opened files first. Although you should really debug why your file is still opened.
pytables (which h5py uses) keeps track of all open files and provides
an easy method to force-close all open hdf5 files.
import tables
tables.file._open_files.close_all()
If you use the with statement you can enforce closing of file, even if a exception occurs:
with h5py.File(filename, 'r+') as f:
f.write(...)

Related

Python: Check if the file can be read before processing the file

I am using a watchdog module to move video files to a different folder. But the problem is the move process starts as soon as the file is created and is still under copy process from its origin location. To overcome this, I did a file size check, but windows is not refreshing the filesize in real time and hence the file size check is not proving to be useful.
Is there another way to check if the file is fully copied (maybe if file can be read/opened?) prior to moving the file. I tried the code below but to no avail.
while True:
try:
with open(os.path.basename(event.src_path)) as f:
s = f.read()
print ("can read the file now")
except IOError:
print ("cannot read the file")
#shutil.move operation here
Any ideas will be appreciated.

Does try close the file after if it is successful?

I am trying to implement a quick code that checks to see if an Excel file is open. When the file is open, the IOError works and tells me the file is open. I can close and reopen the file at this point. If I do not have the file open, and either by calling a subprocess.call() or simply clicking on it, I an error.
Why does the try corrupt it if there is no error raises?
I have tried to close the file after the try but this does not work either.
file_path = (r'C:\users\Desktop\Build-Temp.xlsx')
if os.path.exists(file_path) is True:
report_closed = True
try:
report_opcl = open(file_path, 'w+')
# report_opcl.close()
except IOError:
print("file open already")
report_closed = False
# if report_closed is True:
# with report_opcl:
# subprocess.call(file_path,shell=True)
Errors
Excel cannot open the file 'Build-Temp.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Opening a file with 'w' will delete the file contents. The docs call this "truncating". You might avoid the corruption using 'a' instead (not tested).
Anyway this is not going to work. By the time you check that report_closed is true another process might have opened the file.
I think you are trying to solve an unsolvable problem. There is no way to make sure that a file (identified by its path) will be available to another process in the future.

having trouble opening a csv file in web2py

I'm working on a project in web2py where I need to upload a csv file to a database, then take the information that is in that file and do something with it. I am able to upload a csv file, and in the database I can click on this file, open it, and physically read it, but when I try to open and read it in the controller it doesn't work.
The code in my controller that causes the error looks like this:
csvfile = open(form.vars.csv, 'r')
the error is "FileNotFoundError: [Errno 2] No such file or directory: 'datab.csv.aae72db13bc450af.637376746573742e637376.csv'
"
why is this not working?
After the form has been processed, the upload field in the database, and therefore form.vars.csv, simply stores the filename, not the full file path. Rather than manually constructing the full file path and calling open, more simply you can just do:
filename, csvfile = db.mytable.csv.retrieve(form.vars.csv)
When passed a file name, the .retrieve method of an upload field object returns the original (untransformed) file name as well as the open file object.
See http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#More-on-uploads.

python 2.7: Testing text file in idle

I m using the command testFile = open("test.txt") to open a simple text file and received the following: Does such errors occur due to the version of python one uses?
IOError: [Errno 2] No such file or directory: 'Test.txt
add an "a+" mode argument on to your open, this will create the file if it doesn't exist:
testFile = open("test.txt", "a+")
Syntax for opening file is:
file object = open(file_name [, access_mode][, buffering])
As you have not mentioned access_mode(optional), default is 'Read'. But if file 'test.txt' does not exists in the folder where you are executing script, it will through an error as you got.
To correct it, either add access_mode as "a+" or give full file path e.g. C:\test.txt (assuming windows system)
The error is independent from the version, but it is not clear
what you want to do with your file.
If you want to read from it and you get such an error, it means that your file is not where you think it is. In any case you should write a line like testFile = open("test.txt","r").
If you want to create a new file and write in it, you will have a line like
testFile = open("test.txt","w"). Finally, if your file already exists and you want to add things on it, use testFile = open("test.txt","a") (after having moved the file at the correct place). If your file is not in the directory of the script, you will use commands to find your file and open it.

Saving data into a text file

def ConvertFile():
FileNameIn = 'Hexdata.dat'
HexFile = open(FileNameIn, 'r')
for Line in HexFile:
print (Line)
print (Binary(Line))
HexFile.close()
So far I have that, which, when the program is run, converts the Hexidecimal number in the file to binary. This is in a file called Hexdata.dat
What I want to do is then save the binary output into a file called Binarydata.dat
How would I approach this in code? Be aware I'm new with Python and haven't covered this properly. I've tried different bits of code but they've all been unsuccessful, as really, they're all guesses.
I'm not asking you to solve the problem for me, but more asking how I would save the output of a program into a new text file.
You're already most of the way there. You already know how to open a file for reading:
HexFile = open(FileNameIn, 'r')
The 'r' there means "open for reading". If you look at the documentation for the open function, you will see that replacing the r with a w will open a file for writing:
OutputFile = open(FileNameOut, 'w')
And then you can send output to it like this:
print >>OutputFile, "Something to print"
Or use the write method on the file object:
OutputFile.write("Something to print\n")
Read the documentation of the open function (to open the file in write mode) and File Objects (to write information to the opened file).
You have to have 2 files in this script. The one you're reading from and the one you're writing to. Use the option wb (write binary) when opening the file you are going to write into. These two links should help a beginner with little or no Python knowledge complete your exercise: Intro to File Objects and Tutorial on File I/O.
You are currently opening the file in reading mode, so in order to write to the file, you would want to open the file with the buffering mode as ('w'). Quote from: http://docs.python.org. You can do so easily by replacing your 'r' with 'w'.
'w' for writing (truncating the file if it already exists
For more reference see open(name[, mode[, buffering]])
# the file name
FileNameIn = 'Hexdata.dat'
# create a file object: open it with "write" mode
HexFile = open(FileNameIn,"w")
for line in HexFile:
HexFile.write(Binary(line))
HexFile.close()
Have you tried using open('Binarydata.dat', 'w') for writing to the file? There are plenty of ways to write to a file, most of which can be found here: http://docs.python.org/tutorial/inputoutput.html

Categories