Python Forum
delete a file after closing it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete a file after closing it
#1
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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete multiple lines from txt file Lky 6 2,202 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Find and delete above a certain line in text file cubangt 12 3,353 Mar-18-2022, 07:49 PM
Last Post: snippsat
  How to delete portion of file already processed? Mark17 13 2,635 Jan-22-2022, 09:24 AM
Last Post: Pedroski55
  my python file closing rwahdan 1 1,417 Jun-30-2021, 08:25 AM
Last Post: Gribouillis
  delete a file works but with error Leon79 4 2,873 Jul-14-2020, 06:51 AM
Last Post: snippsat
  Find, delete and add text into pdf file a_shvechkov 2 5,820 Jul-08-2020, 10:50 AM
Last Post: a_shvechkov
  Delete all contents of a file from the fifth line? PythonNPC 1 1,873 Apr-18-2020, 09:16 AM
Last Post: buran
  How To Find an Opening and Closing String, Copying Open/Close/Contents to New File davidshq 1 1,988 Mar-03-2020, 04:47 AM
Last Post: davidshq
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 4,029 Dec-10-2019, 11:09 PM
Last Post: Cosmosso
  delete file with handling 3Pinter 1 2,069 Oct-17-2019, 04:06 PM
Last Post: 3Pinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020