Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

image - How can I create n number of files in python?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

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

Edit: removed my wrong explanation on +


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...