从文本文件中读取路径?

我试图运行在Python中编写一个程序,它从文本文件中读取路径并删除其中列出的所有文件。

文本文件包含新文件的完整路径,每个文件路径位于新行。 即:

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

不知道我该怎么做。


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

{您应该对您不关心的文件进行测试以避免不当操作}

要阅读文本文件,你可以这样做:

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

我假设你在文件中每行有一个条目

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

上一篇: Reading paths from a text file?

下一篇: Delete specific row and corresponding file in python