Solved

Delete file if exist

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

How do I delete a file if it exists?

Cheers.
0
Comment
Question by:chipped
  • 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 28

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now