Posts python中使用with打开多个文件
Post
Cancel

python中使用with打开多个文件

第一种写法

1
2
3
with open('file1', 'r') as f1, open('file2', 'r') as f2, open('file3', 'w') as f3:
    f3.write(f1.read())
    f3.write(f2.read())

第二种写法

1
2
3
4
5
from contextlib import nested

with nested(open('file1', 'r'), open('file2', 'r'), open('file3', 'w')) as (f1,f2,f3):
    f3.write(f1.read())
    f3.write(f2.read())

真诚邀请您走进我的知识小宇宙,关注我个人的公众号,在这里,我将不时为您献上独家原创且极具价值的技术内容分享。每一次推送,都倾注了我对技术领域的独特见解与实战心得,旨在与您共享成长过程中的每一份收获和感悟。您的关注和支持,是我持续提供优质内容的最大动力,让我们在学习的道路上并肩同行,共同进步,一起书写精彩的成长篇章!

AI文字转语音
AI个性头像生成
This post is licensed under CC BY 4.0 by the author.

Trending Tags