How to clean file using Python

In the Unix shell, I can do this to delete the file:

cd /the/file/directory/ :> thefile.ext 

How will I do this in Python?

Is os.system here, I would not know how I would have to send 2 actions one after another, i.e. cd and then :> .

+52
python
Feb 06 '11 at 15:44
source share
3 answers

Opening the file creates it and (if append ('a') is not installed) overwrites it with an empty one, for example:

 open(filename, 'w').close() 
+133
Feb 06 2018-11-11T00:
source share
— -

Alternative Response Form by @rumpel

 with open(filename, 'w'): pass 
+21
Jun 02 '15 at 18:43
source share
 os.remove("filename") file1 = open("filename", "w") 
0
Jan 16 '19 at 16:29
source share



All Articles