Python open() 练习及扩展

170 阅读1分钟
# 使用.split更改元素
s = ['a.png', 'b.jpg', 'c.jf', 'f.jpeg', 'gg.jpg']

with open('picture.txt', 'w') as f:
    for i in s:
        pic_list = i.split('.')
        new_pic = pic_list[0] + '.jpg'
        f.write(new_pic + '\n')


# 创建文件data.txt, 文件共1000行,每行存放一个1-100之间的数字
import random
with open('data.txt', 'w') as f:
    for o in range(1000):
        f.write(str(random.randint(1, 100)) + '\n')

# 写一个程序,任意输入多人的名字,电话号码,家庭住址存入info.txt中,文件格式自己定义,
# 完成后查看文件格式是否是自己需要的格式,
# 写一个程序读取info.txt,以表格的形式打印这些内容

count = "1"
while(count):
    name = input('请输入姓名:')
    number = input('请输入电话:')
    places = input('地址:')
    count = input('是否继续输入,继续请按“1”,退出请按任意键。')
    with open('info.txt', 'a') as f:
        f.write(name + ',' + number + ',' + places + '\n')