如何在python中创建n个文件? [英] How can I create n number of files in python?

查看:147
本文介绍了如何在python中创建n个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设用户给出一个数字n = 3 然后我必须动态创建3个文件.我该怎么做?这些文件的名称可以是什么.具体来说,我要创建n个.jpg文件.

say user gives a number n=3 then I have to create 3 files dynamically. How will I do that? What can be the names of those files. Specifically I want n number of .jpg file created.

推荐答案

假设您已经以某种格式存储了图像(也许是64位字符串?),则可以执行以下操作:

Assuming you have an image stored in some format (maybe base 64 string?) already, you can do something like:

n = raw_input("Number of files: ")
image_list = ... # your logic for the image data here
n = int(n)
for i in range(n):
    image = open("image" + str(i) + ".jpg", "w")
    image.write(image_list[i])
    image.close()

为清楚起见,w表示写入filename(覆盖其内容).如果要附加到文件,请使用a.

For clarification, w means write to filename (overwriting its contents). If you want to append to a file instead, use a.

删除了我对+

这篇关于如何在python中创建n个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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