Python Forum
delete a file works but with error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete a file works but with error
#1
Hello,

I get an error from my code, I create the path in order to delete the correct file (I have import os of course), even if my code delete the correct file, in the terminal I get the following error:

ERROR WHILE DELETING THE FILE.

It doesn't write file deleted, but it deletes the file.

Why I get this error if the code delete the file anyway?


myfile = "test.png"
currentdir = os.getcwd()
path = currentdir + "/static/images/" + myfile

if os.path.exists(path):
    print("File deleted")
    os.remove(path)
else:
    print("ERROR WHILE DELETING THE FILE.")
Reply
#2
without seeing the details, you can delete a file only once.
Assuming that is it there the first run, I do not see you rewriting it, so
it can be deleted again.
Instead of if .. else, you may also do a try...except...

my 2 cts,
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Jul-13-2020, 05:31 PM)DPaul Wrote: without seeing the details, you can delete a file only once.
Assuming that is it there the first run, I do not see you rewriting it, so
it can be deleted again.
Instead of if .. else, you may also do a try...except...

my 2 cts,
Paul



Yes of course it's the first attempt, when the code deletes it I add it back to try again.

Now I followed your suggestion, I just changed it like this:


myfile = "test.png"
currentdir = os.getcwd()
path = currentdir + "/static/images/" + myfile

try:
    os.remove(path)
    print("File deleted")
except:
    print("ERROR WHILE DELETING THE FILE.")
And I get both the messages:

File deleted
ERROR WHILE DELETING THE FILE.

Why this? It simply needs to delete a file... I can't understand Confused

Now it works thank you:


try:
os.remove(path)
print("File deleted")
except:
print("ERROR WHILE DELETING THE FILE.")
Reply
#4
Hi,

In the except: clause, you print your own message,
but tthe idea is that you let python determine what the problem is.
Maybe it is something else !
So you might do this:
except Exception as e:
    print (e.args)
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
The first code should work.
Try in a other environment,here a repl.it link.
Using os.path.join() or PurePath pathlib can be better when joining paths.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  DF.groupby(col).min works, mean gets a "not implemented" error samgardner5 3 446 Feb-29-2024, 06:13 PM
Last Post: deanhystad
  Delete multiple lines from txt file Lky 6 2,279 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,359 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 3,452 Mar-18-2022, 07:49 PM
Last Post: snippsat
  How to delete portion of file already processed? Mark17 13 2,725 Jan-22-2022, 09:24 AM
Last Post: Pedroski55
  Python Regular expression, small sample works but not on file Acernz 5 2,920 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Function throws error but then works? Milfredo 10 3,772 Sep-12-2020, 05:16 AM
Last Post: Milfredo
  Find, delete and add text into pdf file a_shvechkov 2 5,923 Jul-08-2020, 10:50 AM
Last Post: a_shvechkov
  Delete all contents of a file from the fifth line? PythonNPC 1 1,900 Apr-18-2020, 09:16 AM
Last Post: buran
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 4,115 Dec-10-2019, 11:09 PM
Last Post: Cosmosso

Forum Jump:

User Panel Messages

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