繁体   English   中英

Python - 读取文件 - 增量编号 - 保存文件

[英]Python - read file - increment number - save file

我希望你能帮助一些非常简单的事情,但我显然没有清楚地解决它。

我有一堆图片(image1.jpg、image2.jpg、image3.jpg……你明白了。)

我有一个 python 脚本,我想读取一个包含上次发送的图像编号的文件,将其递增 1,并使用新编号保存文件,因此下次运行时,它会拾取并递增下一个号码。 Image1 变成 Image 2 等等

我做了一些环顾四周,惨遭失败,使我认为很简单的事情过于复杂。

有人能把我从折磨中解救出来吗??

感谢大家!!

with open('file.txt','r+') as f:
    data = int(f.read())
    data += 1

    f.seek(0)
    f.write(data) # write the data at the beginning of the file
    f.truncate()  # removes any other data

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM