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 in Python by Sarwar
• 130 points
61 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 by Kalgi
• 42,330 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,810 points
200 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,420 points
121 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
2,702 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 in Python by Nymeria
• 3,520 points
83 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 in Python by Waseem
• 4,470 points
29 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,720 points
2,208 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
787 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27 in Python by rajesh
• 1,210 points
73 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 in Python by Kalgi
• 42,330 points
131 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
487 views