当输出文件存在于路径中时创建输出文件
如何在 python 中编写当输出文件存在于路径中时,输出文件将自动为“originalname”+“_1”/“originalname”+“_2”等选项?
How do I code in python the option that when the output file exists in the path, the output file will automatically be "originalname"+"_1" / "originalname"+"_2" and so on ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
类似的内容,
应该会生成
如果您使用 %s_%3i"
然后您应该得到它,然后按字母顺序列出(但当 i>=1000 时会出现问题)
Something like
This should generate
if you use %s_%3i" you should get
which will then list alphabetically (but have problems when i>=1000)
您可以使用
os.path.exists
检查文件是否已存在。剩下的就是一个尝试新文件名的简单循环。
You can use
os.path.exists
to check if a file already exists. The rest is a simple loop that tries new filenames.isfile 检查文件是否存在并也进入 simlinks;您可以使用完整的文件路径。
isfile checks for the existence of a file and goes down simlinks as well; you can use the full filepath.