当KeyboardInterrupt时,Python在文件上保存一个变量

时间:2017-07-14 11:54:33

标签: python

让我提前感谢你的帮助

我想知道是否有人可以帮助我,当我按下Ctrl + c时,我希望它打开文件 keepline 并用 keeponline 覆盖其中的内容(它在main()上递增。但是我无法从main()获取值到除外,有人可以帮助我吗?再次感谢

全球热线电话

def main():

def signal_handler(signal, frame) :
    print('You pressed Ctrl+C!')

    print "Keeponline is", keeponline

    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

try:
    keepline = open("path", "r+")
    keeponline = keepline.readline()
    keeponline = int(keeponline)
    keepline.close()
except OSError:
    print "[*] File not found."

errorimg = "path"
lslinks = "path"

savepath = tkFileDialog.askdirectory(parent=root, initialdir="/", title='Please select a directory')
if savepath == '':
    print "[*] The path to save can't be empty!"
    sys.exit()

driver = webdriver.PhantomJS(r"path")  #
driver.set_window_size(1600, 900)

count = 0
y = 1
z = 500
perfolder = 500
url = 'https://prnt.sc/'

def getfiles(count, keeponline, perfolder, y, z):
    with open('path', "r") as f:
        for i in xrange(keeponline):
            f.next()
        for line in f:
            keeponline += 1
            link = url + line
            driver.get(link)
            spage = driver.page_source  # save the page's html
            if errorimg in spage:
                print "Removed!"
                continue
            else:
                if count < 501:
                    if not os.path.exists(savepath + lslinks + ("(%s-%s)" % (y, z))):
                        os.makedirs(savepath + lslinks + ("(%s-%s)" % (y, z)))
                    driver.save_screenshot(
                        savepath + lslinks + ("(%s-%s)" % (y, z)) + "\\" + line[:-1] + ".png")
                    print "Saved!"
                    print keeponline
                    count += 1
                else:
                    y = y + perfolder
                    z = z + perfolder
                    count = 1
    keepline.close()
    f.close()
    driver.close()

while os.path.exists(savepath + lslinks + ("(%s-%s)" % (y, z))) == True:
    files = len(fnmatch.filter(os.listdir((savepath + lslinks + ("(%s-%s)" % (y, z)))), '*.txt'))
    if files < 500:
        count = files
        getfiles(count, keeponline, perfolder, y, z)
    else:
        y = y + perfolder
        z = z + perfolder
        if not os.path.exists(savepath + lslinks + ("(%s-%s)" % (y, z))):
            getfiles(count, keeponline, perfolder, y, z)
            break

if os.path.exists(savepath + lslinks + ("(%s-%s)" % (y, z))) == False:
    getfiles(count, keeponline, perfolder, y, z)

1 个答案:

答案 0 :(得分:0)

我不确定自己到底需要什么,

compile 'com.android.support:design:25.3.1'