文件读改写(06)

学校作业 专栏收录该内容
6 篇文章 1 订阅

1. Write a Python program to read 5 random lines from file A and then write them to file B.

import random 


with open('A.txt') as task_A:
    A_lines=task_A.readlines()
    l=len(A_lines)



with open('B.txt','w') as task_B:   
    for i in range(5):
        r = random.randint(0,l+1)
        print(A_lines[r],file=task_B)

2. Ask users to enter their name and gender first. Then print it to the screen for user confirmation. After that, append the name and gender to file user_info.txt as a new line.

list=[]
name=input('请输入你的姓名')
sex=input('请输入你的性别')
list.append(name)
list.append(sex)

with open('info.txt','a') as output:
    line=name+':'+sex+'\n'
    output.write(line)  

代码段稍有简陋,得闲改进

2021.09.27

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值