再考虑一个场景,要读取文件内容,并把年龄和名字的顺序交换存成新文件age_name.txt,这时可以同时打开两个文件:
with open('name_age.txt', 'r') as fread, open('age_name.txt', 'w') as fwrite:
line = fread.readline()
while line:
name, age = line.rstrip().split(',')
fwrite.write('{},{}\n'.format(age, name))
line = fread.readline()

随机推荐

  1. macOS SIP 权限设置

    1.macOS SIP 权限设置 对于 macOS 10.11+ 用户,由于系统启用了 SIP(System Integrity Protection), 导致 root 用户也没有权限修改 /usr ...

  2. 【C语言】练习3-5

     题目来源:<The C programming language>中的习题P51  练习2-1:  编写函数itob(n, s, b),将整数n转换为以b为底的数,并将转换结果以字符的形 ...

  3. JQuery EasyUI Combobox的onChange事件

    html中的select 的change事件 <select id="consult_province" name="consult_province" ...

  4. MySQL复制的管理和维护

    1.查看主库 mysql> show master status; mysql> show master status; +------------------+-----------+- ...

  5. 5.翻译:EF基础系列---EF中的上下文类

    原文地址:http://www.entityframeworktutorial.net/basics/context-class-in-entity-framework.aspx EF中的上下文类是一 ...

  6. Android调用系统软键盘

    /** * * @MethodName:closeInputMethod * @Description:关闭系统软键盘 * @throws */ public void closeInputMetho ...

  7. WGAN (原理解析)

    在GAN的相关研究如火如荼甚至可以说是泛滥的今天,一篇新鲜出炉的arXiv论文<Wasserstein GAN>却在Reddit的Machine Learning频道火了,连Goodfel ...

  8. GANS 资料

    https://blog.csdn.net/a312863063/article/details/83512870 目 录第一章 初步了解GANs 3 1. 生成模型与判别模型. 3 2. 对抗网络思 ...

  9. MySQL 主外键约束与标准SQL不同的地方

    [标准SQL的外键约束条件] 1): 子表引用父表的主键 drop table if exists child,parent; create table if not exists parent( i ...

  10. vs 2017打包安装包(印象深刻)

    Visual Studio Install 打包安装项目2017 以下是具体步骤 一.安装环境 1.下载:链接地址 2.安装vs2017的时候需要安装依赖 .NET framework 4.6 .下载 ...