5

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)

CodeIt
  • 3,492
  • 3
  • 26
  • 37
Malacophonous
  • 77
  • 1
  • 1
  • 4
  • 2
    Have you checked whether or not os.remove() actually does send things to the Trash? – Finomnis Jul 21 '19 at 08:46
  • I don't know why os.remove() was sending files to Trash/Recycle Bin earlier, but it seems to be working as expected now .. Thanks for the answers !!! – Malacophonous Jul 24 '19 at 05:10

3 Answers3

15

os.remove is already what you're looking for. It doesn't send things to Trash. It just deletes them.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • 1
    Which python module send file to trash? Trash means Recycle Bin? – PySaad Jul 21 '19 at 04:36
  • @py_saad: I don't think Python comes with any dedicated utility to do that. As for what Trash is, the questioner is probably on OS X or some similar system where the Recycle Bin equivalent is called Trash, or maybe they're on Windows and just using the wrong name. – user2357112 Jul 21 '19 at 04:38
  • 1
    If you want to send something to the Recycle Bin or other equivalent location, you'd probably have to explicitly move the file to whatever hidden folder is used to store Recycle Bin contents (or Recycle-Bin-equivalent contents). – user2357112 Jul 21 '19 at 04:43
7

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)

Source

You can read more about the difference between os.remove() and os.unlink below.

os.remove(path):

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.

os.unlink(path):

Remove (delete) the file path. This is the same function as remove(); the unlink() name is its traditional Unix name.

Availability: Unix, Windows.

Source

CodeIt
  • 3,492
  • 3
  • 26
  • 37
-3

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!

Jiang1926
  • 1
  • 1
  • 1
    If for whatever reason you use this approach, absolutely NEVER allow user input to be piped into this call, e.g. `os.system(f'rm {some_user_input}')`. This is every hacker's dream. – TheHiggsBroson Jul 11 '22 at 21:47