try:
f = open("file.txt", "r")
try:
string = f.read()
line = f.readline()
lines = f.readlines()
finally:
f.close()
except IOError:
pass
try:
f = open("file.txt", "w")
try:
f.write('blah') # Write a string to a file
f.writelines(lines) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass
你好,
这是我可以读写文件的模式,但我想打开文件一次并在 python 中执行读写操作