# 读写txt文本文件
# 1.打开文件
# 使用Python内置的open打开txt文件
# open('要打开的文件名称', mode=' ')
# w 只能操作写入
# r 只能读取
# a 向文件追加
# w模式打开文件,如果文件中有数据,再次写入内容,会把原来的覆盖掉
# file_handle = open('1.txt', mode='w')
# 2.向文件中写入数据
# file_handle.write('hello world\n')
# 2.1. writelines( )函数,会将列表中的字符串写入文件中,但不会
#自动换行,如果需要换行的话,手动添加换行符
file_handle.writelines(['hello', 'world', '你好', '世界'])
#3.关闭文件
file_handle.close( )
# 读取txt文件文本
# 1.打开文件
# 使用r模式打开文件,做读取文件操作
# 打开文件模式,默认的就是r模式,如果只是对文件,可以不填
#写mode模式
file_handle = open('1.txt', mode='r')
# 2.读取文件
# 2.1 read(int)函数 默认读取文件内容,如果指定读取长度,
#会按照长度去读取,不指定读取所有数据
# content= file_handle.read( )
# print(content)
# 2.2 readline( )函数 默认读取文件中的一行数据
content = file_handle.readline( )
print(content)
# 2.3 readlines( )函数 读取所有行的数据,会把每一行的数据作为
# 一个元素,放到列表当中
# contents = file_handle.readlines( )
#print(contents)
# 3.关闭文件
# file_handle.close( )