从循环保存文件而不会覆盖 [英] Saving Files from a Loop Without Overwrite

查看:97
本文介绍了从循环保存文件而不会覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我的python脚本(使用"Spynner")具有一个包含以下内容的循环:

Currently my python script (which uses 'Spynner') has a loop which contains:

browser.load(url)
time.sleep(5)
browser.snapshot().save('example.png')

...但是问题在于,每次循环循环时,它都会使用新呈现的URL的图像(每次循环运行时URL都会更改)覆盖以前存储的图像,而不是制作一个新的图像,与前一个.我创建了一个计数,但是由于某些原因:

...but the problem is that every time the loop cycles it overwrites the previously stored image with an image of the newly rendered URL (the URL changes each time the loop is run) instead of making a new image, separate from the previous one. I have created a count but for some reason:

browser.snapshot().save((count)'.png')

似乎不起作用.如何使用.save将定义的计数用作文件名的一部分?

doesn't seem to work. How do you use a defined count as part of a filename with .save?

谢谢

推荐答案

browser.snapshot().save('{:03d}.png'.format(count))

应该这样做.如果需要,可以将格式运算符(:03d)更改为更大的数字.

should do it. You can change the format operator (:03d) to a bigger number if you need to.

这将为您提供格式为000.png001.png等的文件.仅使用str(count)+'.png'不会在前面填充零,从而使以后处理文件更加困难

This will give you files with the format 000.png, 001.png, etc. Just using str(count)+'.png' will not pad the zeroes at the front, making for more difficult file handling later

这篇关于从循环保存文件而不会覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆