helloFile = open('./hello.txt') # 默认是读模式
helloContent = helloFile.read()
helloFile.close()
print(helloContent)
# 以使用 readlines()方法,从该文件取得一个字符串的列表。
# 列表中的每个字符串就是文本中的每一行
helloFile = open('./hello.txt')
helloContent = helloFile.readlines()
helloFile.close()
print(helloContent) # ['hello\n', 'world']
# 写入文件
baconFile = open('./bacon.txt', 'w') # 文件不存在会创建
baconFile.write('Hello world!\n')
baconFile.close()
# 追加
baconFile = open('./bacon.txt', 'a')
baconFile.write('Bacon is not a vegetable.')
baconFile.close()
baconFile = open('./bacon.txt')
context = baconFile.read()
baconFile.close()
print(context)