Python Forum

Full Version: delete a file after closing it
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I used a file in my code
and I wanted to delete it
manually.
A message appeared saying that it was still open
in python and therefore I could not.

I used 'file.close()'
but I could still not delete it.

In the end I had to close the whole compiler in order to be able
to do so.
What should I do in order to manually delete a file without
closing the compiler?
Your better off using the with clause:
example:
with open(filename, 'r') as fp:
    for line in fp:
        ...
No close needed, it's done automatically.