?
Solved

Delete file if exist

Posted on 2011-03-01
6
Medium Priority
?
852 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 400 total points
ID: 35009641

import os;

try :
  remove( filename );
except :
  pass;

Open in new window

0
 
LVL 29

Accepted Solution

by:
pepr earned 1600 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to dynamically set the form action using jQuery.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

770 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