简体   繁体   中英

How to Open/write/close multiple files in a loop?

Example:

f = open (path/pdf1.pdf)
f1 = open (path/pdf2.pdf)
f2 = open (path/pdf3.pdf)

Can the above be ran in a loop?

Yes, here is one implementation of a loop (f string needs python 3):


for x in range(1,4):
    f = open(path/ (f"pdf{x}.pdf"))

Note that this doesn't do anything with the file though. This also doesn't follow best file practices (really should use a context manager). Can't be more specific without know what you want to do with the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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