t1=open(filename,'w+')
line1=input('please type some code'+'\n'+'>')
line2=input('type someother code'+'\n'+'>')
t1.write(line1+'\n')
t1.write(line2)
print(t1.readline())
print(t1.read())
t0=open(t1,'a+')
line3=input('please append some code'+'\n'+'>')
TypeError: invalid file: <_io.TextIOWrapper name='testing.txt' mode='w+' encoding='UTF-8'>
q1/以'w'模式open了一个文件,如果再次以某种模式(例如本文打'a+'模式)open,需要怎么做?还是说,python根本不支持连续open?我是刚学python,知道很少。
我同学说,先 close,那显然不行啊,close后不能再对文件进行任何操作了。
我的目标是先open(filename,'w+')写入两行字符串line1 line2,然后再追加line3.
这个题目是我自己随便写的,目的是为了练习。
open 的参数传错了,所以报的错,传的应该是个文件路径,应该是个
str
类型的。你可以试下: