笨方法学python 习题16

读写文件的函数

函数作用
close关闭文件。跟编辑器的“文件”→“保存”是一个意思
read读取文件的内容。你可以把结果赋给一个变量
readline只读取文本文件的一行
truncate清空文件
wirte(‘stuff’)将stuff写入文件
seek(0)将读写位置移动到文件开头

代码如下:

from sys import argv

script, filename = argv,'filename'

print(f'''we're going to erase {filename}.''')
print('''If you don't want that,hit CTRL-C (^C). ''')
print('If you do want that,hit RETURN.')

input('?')

print('Opening the file...')
target = open(filename,'w')

print('Truncating the file. Goodbye!!!!!!!!!!!')
target.truncate()

print('''Now I'm going to ask you for three lines.''')

line1=input('line 1:')
line2=input('line 2:')
line3=input('line 3:')

print('''I'm going to write these to the file.''')

target.write(line1)
target.write('\n')
target.write(line2)
target.write('\n')
target.write(line3)
target.write('\n')


print('and finally, we close it.')
target.close()

结果输出

we're going to erase filename.
If you don't want that,hit CTRL-C (^C). 
If you do want that,hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!!!!!!!!!!!
Now I'm going to ask you for three lines.
line 1:Marry had a little lamb
line 2:It's fleece was white as now
line 3:It was also tasty
I'm going to write these to the file.
and finally, we close it.

进程已结束,退出代码0

在这里插入图片描述

巩固练习

1. 用read和argv读取刚才新建的文件

from sys import argv
#调用sys模块的argv函数
scripts,filename = argv,'filename'
#参数命名,如果不命名filename参数,就无法读取txt
txt = open(filename)
#命名变量txt是打开打开filename,而filename的参数是'ex15_sample.txt'
print(f"Here's your life{filename}:")
#用(f'')显示字符串时,将把每个变量都替换为其值,替换值:{filename}
print(txt.read())
#read函数可以读取txt文件
print("Type the filename again:")
file_again = input(">")
txt_again = open(file_again)
#again函数,没查到怎么用,估计就是重复一下的意思喽
print(txt_again.read())

输出:

Here's your lifefilename:
Marry has a little lamb
It's fleece was white as now
It was also tasty

Type the filename again:
>filename
Marry has a little lamb
It's fleece was white as now
It was also tasty

2. 用一个target.write()将line1、line2、line3打印出来,替换掉原来的6条代码

from sys import argv

script, filename = argv,'filename'

print(f'''we're going to erase {filename}.''')
print('''If you don't want that,hit CTRL-C (^C). ''')
print('If you do want that,hit RETURN.')

input('?')

print('Opening the file...')
target = open(filename,'w')

print('Truncating the file. Goodbye!!!!!!!!!!!')
target.truncate()

print('''Now I'm going to ask you for three lines.''')

line1=input('line 1:')
line2=input('line 2:')
line3=input('line 3:')

print('''I'm going to write these to the file.''')

# target.write(line1)
# target.write('\n')
# target.write(line2)
# target.write('\n')
# target.write(line3)
# target.write('\n')
target.write('{line1},{''\n''},{line2},{''\n''},{line3},{''\n''}')


print('and finally, we close it.')
target.close()

输出

we're going to erase filename.
If you don't want that,hit CTRL-C (^C). 
If you do want that,hit RETURN.
?
Opening the file...
Truncating the file. Goodbye!!!!!!!!!!!
Now I'm going to ask you for three lines.
line 1:1
line 2:2
line 3:3
I'm going to write these to the file.
and finally, we close it.

进程已结束,退出代码0

3. 给‘open’多传一个参数‘w’的原因

‘w’是特殊字符,用于表示文件的访问模式。

w‘写入’(write)
r‘读取’(read)
a‘追加’(append)

4. 文件模式有哪些修饰符?

'+'可以实现‘w+’,‘r+’,‘a+’,把文件用同时读写的方法打开,根据使用的字符,以不一样的方式实现文件的定位。

5. 如果只写open(filename),那就用’r’(只读)模式打开

是的,这是open()函数的默认行为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值