删除文件

时间:2016-06-17 15:38:22

标签: python file

我有这个代码暴力强制参数:

import urllib
import sys

with open('finaltext.txt', 'r') as f:
    for line in f:

        site = urllib.urlopen("localhost/?cin="+line)
    if site.read() == "good":
        print line
        reponse = open("result.txt",'w')
        reponse.write(line)
        reponse.close()
        sys.exit()
    else:
        print line+"NOO"

我想让代码删除文件finaltext.txt,中已经强制执行的每一行,所以如果我停止这个过程,我可以稍后从我停止的地方继续。

1 个答案:

答案 0 :(得分:0)

import urllib
import sys
s = open('index.txt','r')
indx = int(s.readlines()[0])
s.close()
with open('finaltext.txt', 'r') as f:
    g = open('result.txt','w')
    finaltext = f.readlines()
    for line in finaltext[indx:len(finaltext)]:
        indx+=1
        with open('index.txt','w') as ixfile:
            ixfile.write(str(indx))
        site = urllib.urlopen("localhost/?cin="+line)
        if site.read() == "good":
            print line
            g.write(line)
        else:
            print line+"NOO"
    g.close()
    sys.exit()

未测试。我也不认为这是对的,因为我不知道你的任何东西是如何运作的。

编辑:这个怎么样?