Solved

Delete file if exist

Posted on 2011-03-01
6
845 Views
Last Modified: 2012-06-27
hey guys,

How do I delete a file if it exists?

Cheers.
0
Comment
Question by:chipped
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 100 total points
ID: 35009641

import os;

try :
  remove( filename );
except :
  pass;

Open in new window

0
 
LVL 29

Accepted Solution

by:
pepr earned 400 total points
ID: 35010916
Actually, it should be os.remove(filename) -- see http://docs.python.org/library/os.html#os.remove. You may want to use it also without the try/except...

    if os.path.isfile(filename):
        os.remove(filename)

Open in new window


See the http://docs.python.org/library/os.path.html#os.path.isfile
Still, the os.remove() may fail (e.g. for files marked as read-only or if you do not have the right to delete), and you may want to test if this happened.  On the other hand, you should not overcomplicate your code.

Also, there is no need to use the semicolon in the above Python code.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35011883
sigh,,  pepr, of course, you are correct. ;-)  Thanks for catching that.
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 

Author Closing Comment

by:chipped
ID: 35013562
Thank you guys :)
0
 

Author Comment

by:chipped
ID: 35013647
Could you guys pop back in here when you have a chance http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_26852254.html#a35013581

I've pretty much got it all working, just one little kink.

thanks.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35016729
Thanks for the assist, and the points.

Good luck & have a great day.
0

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question