用代码演示Python中的文件操作,readline,readlines,文件操作模式中的r和a模式
要解释的,全部都在代码里面,自己看即可:
#!/usr/bin/python # -*- coding: utf-8 -*- #------------------------------------------------------------------------------- #Function: Demo readline and append mode for file operaton # 【示例】演示Python中的文件操作,以及readline读取单行,readlines读取全部行,详解文件打开模式 # https://www.crifan.com/demo_python_file_operation_readline_readlines_and_file_open_mode # #Author: Crifan Li #Date: 2013-03-13 #------------------------------------------------------------------------------- print "For Python File operations, refer materials:"; print "Input and Output" print "http://docs.python.org/2/tutorial/inputoutput.html"; print "Now demo how to use readline to read single line"; print "and use append mode to append data into an existing file"; print """ in Python, for openedFile = open("yourFilename", "mode"); the mode argument: r : only read(can not write), this is default mode if no mode designated w : only writing (an existing file with the same name will be erased) a : appending(any data written to the file is automatically added to the end) r+ : both reading and writing On Windows, 'b' appended to the mode opens the file in binary mode so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it'll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn't hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files. """; # Open input file inputFile = open("inputFile.txt", "r") print "Name of the input file: ", inputFile.name; # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line #open output file outputFile = open("outputFile.txt", "a"); print "Name of the output file: ", outputFile.name; # Assuming output file already contain: # this is just some original data # should not be overwritten after be written # #method 1: # currentLineContent = inputFile.readline(); # while(currentLineContent): # print "current line content: %s" % (currentLineContent); # currentLineContent = inputFile.readline(); # #append into output file # outputFile.write(currentLineContent); #method 2: allLines = inputFile.readlines(); for eachLine in allLines: print "current line content: %s" % (eachLine); #append into output file outputFile.write(eachLine); #[output] # 1. printout is: # Name of the input file: inputFile.txt # current line content: This is 1st line # current line content: This is 2nd line # current line content: This is 3rd line # current line content: This is 4th line # current line content: This is 5th line #2. now output file contains: # this is just some original data # should not be overwritten after be written # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line # Close opened file inputFile.close(); outputFile.close();
【总结】Python中的文件打开模式的含义
对于Python中:
openedFile = open("yourFilename", "mode");
的mode,即文件打开模式的含义,整理如下:
模式mode | 含义 | 说明 |
r | 只读(不可写) | 如果未指定mode,则默认为此’r’模式 |
w | 只写 | 如果用’w’,会把原先已存在的文件中的内容擦除(清除/冲)掉 |
a | 附加/追加 | 写数据时,会自动添加到(已存)文件的末尾 |
r+ | 即可以读,也可以写 |
注:
- Windows平台:
- 支持在mode后面添加’b’,表示以 二进制 模式打开文件;
- 所以对应的mode就有
- rb
- wb
- r+b
- Python在Windows中,是区分 二进制 和 文本 文件的。
- 当对文件进行数据读写时,行尾(end-of-line)符,会自动发生轻微的变化
- 此种变化,对于文本文件,是没问题的,但是对于 二进制 文件,则很明显,会改变(破坏)原有的数据,即,是有问题的。
- 所以,当你在用Python操作Windows下面的文件的时候,要自己很清楚自己操作的是什么类型的文件,即,是 文本text 还是 二进制binary 文件
- Unix平台:
- 对于’b’,是不影响的
- 换句话说:如果你是用Python操作 二进制文件,则可以通过 加上’b’,得以实现,平台相关的效果,即 既适用于Windows平台,也适用于Unix类的平台。
参考资料
转载请注明:在路上 » 【详解】Python中的文件操作,readline读取单行,readlines读取全部行,文件打开模式