文件读写

来源:互联网 发布:oute66手机导航软件 编辑:程序博客网 时间:2024/06/05 21:17
#文件操作
# 写入
# text = 'this is first line \n this is second line \n this is last line'
# myfile = open('my_file.txt','w')
# myfile.write(text)
# myfile.close()
# 给文件添加内容 'a'=append 以增加内容的形式打开
addText = '\nthis is add text'
myfile = open('my_file.txt','a')
myfile.write(addText)
myfile.close()

#读取文件内容
file = open('my_file.txt','r');
content = file.read();
print(content)


#读取文件内容
#file = open('my_file.txt','r');
#content = file.read();
#print(content)

# 足行读取
file = open('my_file.txt','r');

contents = file.readlines()
# 迭代读取的内容
for line in contents:
    print(line)
content = file.readline()
print(content,"hql")




0 0
原创粉丝点击