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
381 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
• 51,890 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,850 points
7,376 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
251 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,520 points
6,679 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,520 points
179 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
66 views
0 votes
1 answer
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 in Python by Gitika
• 33,110 points
83 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 3, 2018 in Python by charlie_brown
• 7,780 points
5,537 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
• 51,890 points
1,391 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
575 views