Python Forum
[Tkinter] remove file from current project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] remove file from current project
#1
Hi,

I created a file and after done it is opened and saved by user. I then need to delete from my current directory in my project. I tried the remove method but not working!

file = str(pdf_file_name) # file name is similar to something.pdf
location= "C:/abc/efg/"
path = os.path.join(location, file)
os.remove(path)
I get this error:
Error:
'something.pdf' is not recognized as an internal or external command, operable program or batch file.
Reply
#2
test if the file exists

this works

from os import path, remove

def file_remover(file):
    if path.exists(file):
        remove(file)
        print('file removed')
    else:
        print('file does not exist')

file_remover('test.pdf')
Output:
file removed
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Please note:
Add after line 3 (only until working) print(f"path: {path}")
try results from command line to make sure it's formed correctly:
Linux: ls -l path
MS Windows: use dir command

Also note, From docs:
Quote: On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.
Make sure file is closed before removing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Unable to get current contents of the entry field where the content is the file path pmpinaki 1 2,193 Apr-18-2020, 06:45 PM
Last Post: deanhystad
  python file(.py) not working with my tkinter project DeanAseraf1 9 6,994 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Problem In calling a Function from another python file of project vinod2810 7 5,218 Oct-05-2019, 01:09 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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