read write mode python
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
python open built-in function: difference between modes a, a+, w, w+, and r+?
号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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中执行读写操作。
与其他编程语言一样,您可以以
r+ 打开读写(不截断,文件指针在开头)w+ 打开进行写入(从而截断文件)和读取a+ 打开进行追加(不截断写入,只在文件末尾,文件指针在文件末尾)和读取
从文件:
r+ : opens the file for both reading and writing
号