Python open语句读写文件

print('file1\n')
a='lalala'
file1=open('file1.txt','w')
file1.write('this is line1\nthis is line2\n')
file1.write(a)
file1.write('\n')
file1.close()

print('\nfile2\n')
file2=open('file1.txt','a')
file2.write('this is last line')
file2.close()

print('\nfile3\n')
file3=open('file1.txt','r')
content = file3.read()
print(content)
file3.close()

print('\nfile4\n')
file4=open('file1.txt','r')
content = file4.readline()
print(content)
content = file4.readline()
print(content)

print('\nfile5\n')
file5=open('file1.txt','r')
content = file5.readlines()
print(content)

输出

file1


file2


file3

this is line1
this is line2
lalala
this is last line

file4

this is line1

this is line2


file5

['this is line1\n', 'this is line2\n', 'lalala\n', 'this is last line']

猜你喜欢

转载自blog.csdn.net/weixin_43880667/article/details/85142495