Solved

Delete file if exist

Posted on 2011-03-01
6
826 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
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.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

746 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

11 Experts available now in Live!

Get 1:1 Help Now