Hi,
I am new to Python language.
I just want to know how to delete files in python?
Please help.
Hi,
I am new to Python language.
I just want to know how to delete files in python?
Please help.
Hi,
I have a similar question but not like you.
Well if you want to delete the file then you can simply use the remove() function in python.
In this way
My question is How to delete a file before writing it in a first place.Code:import os os.remove('path/file.extension')
I mean i want to check if the file exist or not at a particular location & then if it exist then I want the script to delete it & write a new file.
Is this possible?
Please help
Hi Joyjeet,
The best solution to your question is to check if the file exist or not if not write a new file if exist then you simple have to open the file with w mode (like "f = open('filename', 'w')")
This will automatically truncate the existing file.
Well I want to know how to delete a folder will all its contents?
I mean to say how to delete the folder consisting of all the files & sub folders in it?
How to delete full folder with it's contents?
will remove a folder, but it must be empty. You could probably write something with os.remove and os.walk, but there's also;Code:os.remove('path/folder')
which will delete a folder and all of it's contents (don't forget to import shutil)Code:shutil.rmtree('path/folder')
This are he script for delete files in python.
>>> import os
>>> file = 'myfile'
>>> os.remove('hfkg/' + file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: [Error 2] The system cannot find the path specified: 'hfkg/myfile'
Last edited by janysek; 11-08-2010 at 11:24 AM.
Bookmarks