Reading paths from a text file?

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. ie:

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

Not sure how can I do this.


import os
for curr_path in open("infile.txt", "r").xreadlines():
    os.remove(curr_path.strip())

{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

链接地址: http://www.djcxy.com/p/42426.html

上一篇: Python脚本来检测破碎的图像

下一篇: 从文本文件中读取路径?