I want to permanently delete a file i have created with my python code.
I know the os.remove() etc but can't find anything specific to delete a file permanently.(Don't want to fill Trash with unused files)
I want to permanently delete a file i have created with my python code.
I know the os.remove() etc but can't find anything specific to delete a file permanently.(Don't want to fill Trash with unused files)
os.remove
is already what you're looking for. It doesn't send things to Trash. It just deletes them.
Basically both os.remove()
and os.unlink()
are same. Both these commands removes the files permanently. You can use either of them to perform the desired operation.
If you just want to delete a single file, you can use the below code.
os.unlink(filename)
You can use the below code to remove multiple files using extension type.
import os
for filename in os.listdir():
if filename.endswith('.txt'):
os.unlink(filename)
You can read more about the difference between os.remove()
and os.unlink
below.
Remove (delete) the file path. If path is a directory, OSError is raised; see rmdir() below to remove a directory. This is identical to the unlink() function documented below. On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use.
Availability: Unix, Windows.
Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name.
Availability: Unix, Windows.
Maybe you can try the os.system() function. Just include Linux commands between the brackets and the command will be executed.
Example: os.system('rm xxx')
will remove the file named xxx under your working path.
By default, files removed by the rm
command is not recoverable (unless you have manually set it previously) and you needn't worry about the disk space problem.
Hope it helps!