Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a JSON file if an exception occurs

Tags:

python

I am writing a program which stores some JSON-encoded data in a file, but sometimes the resulting file is blank (because there wasn't found any new data). When the program finds data and stores it, I do this:

with open('data.tmp') as f:
    data = json.load(f)
os.remove('data.tmp')

Of course, if the file is blank this will raise an exception, which I can catch but does not let me to remove the file. I have tried:

try:
    with open('data.tmp') as f:
        data = json.load(f)
except:
    os.remove('data.tmp')

And I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MyScript.py", line 50, in run
    os.remove('data.tmp')
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

How could I delete the file when the exception occurs?

like image 725
bitomic Avatar asked Jan 05 '19 20:01

bitomic


2 Answers

How about separating out file reading and json loading? json.loads behaves exactly same as json.load but uses a string.

with open('data.tmp') as f:
    dataread = f.read()
os.remove('data.tmp')

#handle exceptions as needed here...
data = json.loads(dataread)
like image 80
JL Peyret Avatar answered Nov 20 '22 11:11

JL Peyret


I am late to the party. But the json dump and load modules seem to keep using files even after writing or reading data from them. What you can do is use dumps or loads modules to get the string representation and then use normal file.write() or file.read() on the result.

For example: with open('file_path.json'), 'w') as file: file.write(json.dumps(json_data))

os.remove('file_path.json')

Not the best alternative but it saves me a lot especially when using temp dir.

like image 1
Richard-code-gig Avatar answered Nov 20 '22 10:11

Richard-code-gig