How can I delete a file in Python IDLE

+1 vote
Hi Edureka learning community.

This is Gulam Sarwar Alam.Iam learning Python Fundamentals.How can I delete a file in python as i don't see any delete button.My another query is that in C++ we used to write a programme and save it in a file.For example Two programmes i)Sum of two complex numbers ii)Sum of two integers are saved in two different files.In Python Programming for the above two programs do I have to create two different files or I can save it in one file say SUM.py.Please help me in this regard.
Oct 26, 2019 in Python by Sarwar
• 130 points
2,469 views

1 answer to this question.

0 votes

Hey @Starsarwar

You have two different codes, they will be saved with different names obviously and hence different files. But you can always have them under the same directory.

Assignments
  -sumofcomplex.py
  -sumofint.py

Now coming to deleting files in python, its pretty simple. You'll have to use the OS module which will let you access the files on your system. Use the following code:

import os
if os.path.exists("path/of/file/filename.txt"):
  os.remove("filename.txt")
else:
  print("The file does not exist")
answered Oct 28, 2019 by Kalgi
• 52,350 points

Related Questions In Python

+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,890 points
20,009 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,307 views
0 votes
1 answer

How can I parse a YAML file in Python?

Read & Write YAML files with Python ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,560 points
10,474 views
0 votes
1 answer

How can I read numbers in Python from a custom file?

Hi, good question. Let us first assume that ...READ MORE

answered Feb 6, 2019 in Python by Nymeria
• 3,560 points
1,130 views
0 votes
0 answers

how can i read a text file in python?

can you specify the syntax and prequisites ...READ MORE

Apr 4, 2019 in Python by Waseem
• 4,540 points
582 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,388 views
0 votes
1 answer

how can i extact all the links from a website using python and save it in a csv file ?

Hi, @Shubham, Web scraping is the technique to ...READ MORE

answered Jun 16, 2020 in Python by Gitika
• 65,770 points
4,102 views
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,720 points
13,544 views
0 votes
1 answer

Can I build a plagiarism checker using python?

Hey @Alessha, you can use an SDK ...READ MORE

answered Aug 5, 2019 in Python by Kalgi
• 52,350 points
3,467 views
+7 votes
8 answers

Difference for string comparison in Python: 'is' vs. ==

If we use "==" means both variables ...READ MORE

answered Sep 3, 2018 in Python by Parul Raheja
2,440 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP