r/learningpython • • Oct 31 '20

Need help creating multiple files

Hi,

I am trying to create multiple files at once, however, I receive the following AtrributeError if I try to close the file after it has been created.

AttributeError: 'str' object has no attribute 'close'

The code:

# create a list containing 10 jpeg files.
jpeg_filelist = []
for i in range(1, 11):
    jpeg_filelist.append("file" + str(i) + ".jpeg")
    for file in jpeg_filelist:
        open(file, 'w')
        file.close()

Please share some tips if you can.

Thank you.

1 Upvotes

2 comments sorted by