Python Forum
The code seems correct but my files aren't getting deleted
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.
Reply


Messages In This Thread
The code seems correct but my files aren't getting deleted - by taffylim69 - Feb-03-2019, 10:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Thread deleted ChrisOfBristol 0 175 Apr-10-2024, 02:47 AM
Last Post: ChrisOfBristol
  Correct the algorithm of image filter code saoko 6 2,019 May-08-2022, 05:06 PM
Last Post: saoko
  deleted Overdue 0 1,198 Dec-14-2021, 06:57 PM
Last Post: Overdue
  How to preserve x-axis labels despite deleted subplot? Mark17 1 1,938 Dec-23-2020, 09:02 PM
Last Post: Mark17
  my first code, please correct robboc91 4 2,692 Nov-16-2020, 06:32 PM
Last Post: DougBTX
  Parameters aren't seen inside function Sancho_Pansa 8 2,931 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  How to check if video has been deleted or removed in youtube using python Prince_Bhatia 14 11,806 Feb-21-2020, 04:33 AM
Last Post: jehoshua
  Getting the error like function not defined .. suggest correct code raghava 1 2,054 Feb-04-2020, 11:20 PM
Last Post: micseydel
  My if and while loop statements aren't working RedSkeleton007 7 4,691 Oct-21-2017, 07:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020