读写模式python

时间:2012-11-07 08:02:30

标签: python

  

可能重复:
  python open built-in function: difference between modes a, a+, w, w+, and r+?

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中执行读写操作的模式

1 个答案:

答案 0 :(得分:9)

来自doc

  

r+:打开文件进行读写