1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2016
    Rep Power

    Writing deleted file results to a text file.

    I am trying to get a list of the files in the directory that i delete. So
    when the directory is deleted i will get a text file that has the names of
    the files that were deleted in them. It seems to work however it only
    logs one of the files in the list no matter if there is 50 files or just 1??
    it only logs the file i have called copy 2. I also tried to use send2trash
    module where shutil.rmtree is but i get all kinds of errors that will not
    seem to work whats so ever and doesn't log any files when i use that
    module?? I put the print statement at the end because i can't get the
    send2trash module to work so i have it printing the results encase they
    don't get logged to text file like I'm trying to do.

    import os, send2trash, shutil
    location = ("C:\\users\\Jack\\Desktop\\tester\\")
    with open("deleted files.txt", "w") as f:
        for files in os.listdir("C:\\users\\Jack\\Desktop\\tester\\"):
            if files.endswith(".xlsx"):
                shutil.rmtree(dir)          #send2trash.send2trash(location)  This does't seem to work at all.
                f.write(files + os.linesep)
                print("DELETED: " + files)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Rep Power
    cross posted here Writing Results To A Text File. - Python | Dream.In.Code Would suggest you pay attention to the comment that it deletes the entire directory

    Comments on this post

    • Will-O-The-Wisp agrees : Thank you!
    Last edited by dwblas; May 15th, 2016 at 11:38 PM.

IMN logo majestic logo threadwatch logo seochat tools logo