Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 The code seems correct but my files aren't getting deleted
#1
I heard that python can make life easier, I wanted to remove duplicates in folderA by comparing folderB with folderA, so I decided to download python and try coding with python. My code seems correct, however, my files are failing to delete, what's wrong with it?

I tried unlink but doesn't work.
    import os
    
    with open(r"C:\pathto\output.txt", "w") as a:
        for path, subdirs, files in os.walk(r'C:\pathto\directoryb'):
           for filename in files:
             #f = os.path.join(path, filename)
             #a.write(str(f) + os.linesep) 
             a.write(str(filename) + '\n')
    		 
    textFile = open(r'C:\output.txt', 'r')
    
    line = textFile.readline()
    while line:
     target = str(line)
     todelete = 'C:\directorya' + target
     if (os.path.exists(todelete)):
      os.remove(todelete)
     else:
      print("failed")
     line = textFile.readline()
    
    textFile.close()
I want my files deleted, basically folderA contains some files in folderB, and I'm trying to delete it.
Quote
#2
you cannot delete the file while it is still open.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Not quite getting the correct Output from a function twinpiques 3 156 Aug-04-2019, 11:53 PM
Last Post: twinpiques
  read text file and write into html with correct link jacklee26 4 153 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Random access binary files with mmap - drastically slows with big files danart 1 216 Jun-17-2019, 10:45 AM
Last Post: danart
  Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows ChrisPy33 3 404 Jun-12-2019, 05:56 AM
Last Post: ChrisPy33
  Help with finding correct topic in Python learning yahya01 1 282 Jun-06-2019, 05:01 PM
Last Post: buran
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 343 May-18-2019, 06:34 AM
Last Post: heiner55
  Return in correct json format UtiliseIT 3 344 May-13-2019, 11:24 AM
Last Post: snippsat
  How to continue in loop until correct input received sunnyarora 10 476 May-04-2019, 02:37 PM
Last Post: Yoriz
  Scanning files to rename other files in the same folder. msd1391 6 439 Mar-11-2019, 07:19 PM
Last Post: msd1391
  What is the correct type hint when you want to accept Iterable but not Dictionary LadySvetlana 4 293 Mar-05-2019, 07:33 PM
Last Post: LadySvetlana

Forum Jump:


Users browsing this thread: 1 Guest(s)