Monday, April 21, 2025

Export List of Deleted Files to .TXT File

import os
import send2trash as s2t

target = 'c:\\Python38-64\\OLD-BACKUP\\'

file = open('removed-files.txt', 'w')

for x in os.listdir(target):
    if x.endswith('.dll'):
        s2t.send2trash(target + x)
        file.write(target + x + '\n')

file.close()

This script uses the send2trash module to send files with the .dll extension in the c:\Python38-64\OLD-BACKUP\ directory to the trash. The file paths of the deleted files are also written to a removed-files.txt file in write mode.

Here's a breakdown of the script:

  1. import os and import send2trash as s2t import the necessary modules for the script.
  2. target = 'c:\\Python38-64\\OLD-BACKUP\\' sets the target directory where the script will look for files with the .dll extension.
  3. file = open('removed-files.txt', 'w') opens a new file called removed-files.txt in write mode.
  4. for x in os.listdir(target): loops through the files in the target directory.
  5. if x.endswith('.dll'): checks if the current file has the .dll extension.
  6. s2t.send2trash(target + x) sends the file to the trash.
  7. file.write(target + x + '\n') writes the file path of the deleted file to the removed-files.txt file, followed by a newline character (\n).
  8. file.close() closes the removed-files.txt file.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...