python读写文件学习

python读写文件学习

在笨办法学python中,学到了argv
里面有段代码是
from sys import argv

#script,开始书上是另外一个函数,查阅资料,发现去掉这个
#原来是script,filename=argv但是运行不起,查资料是script是多的一个参数,不是函数,删掉就是了
filename= argv#sys是一个软件包
print(“We’re going to erase %r.”)#这里本来有%filename但是加了运行不了,现在还不知道为什么
print(" if you don’t want taht,hit CTRL_C")
print(“if you do want that ,hit enter”)

input("?")

print(“open the file…”)
target = open(‘text.txt’,‘w’)#w相当于对文件的写操作,就是write的意思

print(“Truncating the file. good bye!”)
target.truncate()#清空文件

print(“Now i’m going to ask you for three lines.”)

linel = input("line 1: ")
line2=input("line 2: ")
line3=input(“line 3:”)

print(“i’m going to write these to the file”)

target.write(linel)#写入第一行
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print(“And finally, we close it.”)
target.close()#关闭文件函数

所以说这就是对文件的基本操作
w相当于是写入模式
r表示读取模式
a表示追加模式,在不删除前面的情况下继续添加吧。
还可以r+w什么操作的
同时读写操作都可以
如果只写open(filename)那么就是默认r模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值