0

I am trying to run write a program in Python where it reads paths from a text file and deletes all the files which are listed within it each one.

The text files contain the complete path of the files with each file path on new line. i.e.:

/mnt/1/a.jpg
/mnt/1/b.jpg

Not sure how can I do this.

martineau
  • 119,623
  • 25
  • 170
  • 301
Sami Ullah
  • 19
  • 1
  • 3
  • 1
    Check out the python docs here https://docs.python.org/2/tutorial/inputoutput.html. Once you've tried implementing something, get back to us and we'll be happy to help you out – lordingtar Apr 12 '17 at 03:02
  • Also check this http://stackoverflow.com/questions/6996603/delete-a-file-or-folder-in-python – zhiqiang huang Apr 12 '17 at 03:06

2 Answers2

1
import os
for curr_path in open("infile.txt", "r").xreadlines():
    os.remove(curr_path.strip())
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
0

{You should test this on files that you do not care about to avoid bad manipulation}

To read a text file you can do:

import os
with open('yourfile', 'r') as F:
    for i in F:
        ###i is one entry in your file
        os.remove(i)
        ###this remove your file i

I assume here that you have one entry per line in your file

Astrom
  • 767
  • 5
  • 20