having trouble opening a csv file in web2py - python

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.

Related

Unable to open a previously opened H5

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(...)

How to create a file in a different directory? (python)

I am currently stuck with the following error:
IOError: [Errno 2] No such file or directory: '/home/pi/V1.9/Storage/Logs/Date_2018-08-02_12:51.txt'
My code to open the file is the following:
nameDir = os.path.join('/home/pi/V1.9/Storage/Logs', "Date_" + now.strftime("%Y-%m-%d_%H:%M") + ".txt")
f = open(nameDir, 'a')
I am trying to save a file to a certain path, which is /home/pi/V1.9/Storage/Logs. I am not sure why it can't find it, since I have already created the folder Logs in that space. The only thing being created is the text file. I am not sure if its suppose to join up like that, but I generally tried to follow the stages on this thread: Telling Python to save a .txt file to a certain directory on Windows and Mac
The problem seems to be here:
f = open(nameDir, 'a')
'a' stands for append, which means: the file should already exist, you get an error message because it doesn't. Use 'w' (write) instead, Python will create the file in that case.
If you are creating the file use the write mode w or use a+
f = open(nameDir, 'w')
f = open(nameDir, 'a+')
Use only a append if the file already exist.
Not really an answer to your question, but similar error. I had:
with open("/Users//jacobivanov/Desktop/NITL/Data Analysis/Completed Regressions/{0} Temperature Regression Parameters.txt".format(data_filename), mode = 'w+') as output:
Because data_filename was in reality a global file path, it concatenated and looked for a non-existent directory. If you are getting this error and are referring to the file path of an external file in the name of the generated file, check to verify it isn't doing this.
Might help someone.

Writing to a file errors and permission errors

while True:
try:
enterName = input("Enter the name of the file:") + ".txt"
latinFile = open(enterName,"r")
read = latinFile.readlines()
for lines in read:
store.append(lines.strip())
checkSquare (store)
print ("File:")
for content in store:
print (content)
saveData(store)
I think the problem is with the code for saving the file contents.
The aim is for the code to open the file,read the contents and it checks to see if the format of the file is correct etc and then if that is all true and it works, it will save the file again (saveData) but it will rename the file so that it says in the directory that it has been validated.
However, the code isn't working ( the os.rename part) and also I keep getting a permissionerror and I don't know how to fix it.
Error message:
Traceback (most recent call last):
File "C:\Users\---\Desktop\python\idkll.py", line 44,in <module>
saveData(store)
File "C:\Users\---\Desktop\python\idkll.py", line 18, in saveData
os.rename (fileName,"VALIDATED" + fileName)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'OPENEDve.txt'
You should close your files as soon as you finish reading from them or writing to them. And you should close the file before attempting to rename it. You can either use the file's .close() method, or use a with statement so that files get closed automatically.
Incidentally, your saveData() function should probably not prompt the user for the filename: just pass it the name you already have in enterName. Also, prepending "VALIDATED" to the filename is not a good strategy. It's ok if you're just using relative filenames in the current directory, but it will make a mess of a proper file path.
In response to the update:
saveFile does not contain a file name string, it's a Python object representing your open file (also known as a file handle). The name of that file is the string in enterName. So you need to do something like
os.rename(enterName, enterName + "VALIDATED")
Just swap os.rename (fileName,"VALIDATED" + fileName) and file.close() lines inside saveData() function.
You should close the file prior to trying to rename it. The error is because your program is actually using the file it is trying to rename.

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.

Can't read file from secure_filename(f.filename)

I need to get a binary file from wtforms and store it as bytea in postgresql. And I don't need to store it permanently as a file. From my understanding of the Flask offical doc I shall be able to access the filename through either request.files.['myfile'].filename or secure_filename(f.filename). However, both of them give me a error: IOError: [Errno 2] No such file or directory: u'myuploadpdf.pdf'
f = request.files.['myfile']:
if f and allowed_file(f.filename):
#filename = secure_filename(f.filename)
data = open(f.filename, 'rb').read()
#data = open(filename , 'rb').read()
binary = psycopg2.Binary(data)
open() expects a pathname to the file. Since the file hasn't been saved to disk, no such path exists. :)
What you actually want to do is call f.read() directly. Reading incoming files is covered here.
Also, definitely use secure_filename() if you work with anything on disk. Don't want to open yourself to any directory traversal attacks down the line.
The objects in request.files are FileStorage objects and they have the same methods as normal file objects in python. So to get the contents of the file as binary, try doing this:
data = request.files['myfile'].read()

Categories