Python文件讀寫的方法有以下幾種:
file = open('filename.txt', 'r')
content = file.read()
file.close()
file = open('filename.txt', 'r')
lines = file.readlines()
file.close()
with open('filename.txt', 'r') as file:
content = file.read()
file = open('filename.txt', 'w')
file.write('Hello, World!')
file.close()
lines = ['Line 1', 'Line 2', 'Line 3']
file = open('filename.txt', 'w')
file.writelines(lines)
file.close()
with open('filename.txt', 'w') as file:
file.write('Hello, World!')