一、文件概念
1.文件操作的步骤: # 1、打开文件获取文件的句柄,句柄就理解为这个文件 # 3、关闭文件。 2.文件模式: 读模式(默认) 写模式,追加模式,读写模式,写读模式w+,追加读模式a+ 1)读模式:r,打开的文件不存在会报错;且不能写,以r模式打开文件,f.write()会报错, io.UnsupportedOperation: not writable,因为读模式不支持写2)写模式:w,可以编辑,如果文件不存在,则新建一个文件,如果存在则使用新的内容覆盖原来的内容,但是不能读取 3)追加模式:a ,可以编辑,如果文件不存在,则新建一个文件,如果存在在文件的末尾增加新内容,作用:在文件的末尾增加内容 ,但是仍然不能读。 模式可以叠加 4)读写模式:r+ 文件不存在会报错 ,错误信息:FileNotFoundError: [Errno 2] No such file or directory: 5)写读模式:w+ 读不到内容,因为open的时候已经把原有内容给清空了 6)追加读写模式:a+ 读虽然不报错,但是读不到内容,可以使用seek(0)将文件指针指向文件的开始位置,就可以读到内容,写的时候仍然是在文件的末尾追加写 PS: #只要模式以r开头,文件不存在,open时必报错 错误信息:FileNotFoundError: [Errno 2] No such file or directory: #只要模式以w开头,都会将原有文件内容清空,重新写入新的内容#模式a+开头,读虽然不报错,但是读不到内容,可以使用seek(0)将文件指针指向文件的开始位置,就可以读到内容,写的时候仍然是在文件的末尾追加写 |
模式 | 读 | 写 | ||
r | open for reading (default) | ✔ | ✘ | |
w | open for writing, truncating the file first 如果以w的模式打开文件,在打开文件的同时会将文档中的内容全部清空 所以,慎用 如果是读模式,不能使用read方法,会报不可读的错io.UnsupportedOperation: not readable | ✘ | ✔ | |
x | create a new file and open it for writing 创建一个新的文件等待写入,如果文件已经存在会报错,如下所示: FileExistsError: [Errno 17] File exists: 'F:\\lp_test\\besttest\\auto_test\\homework\\syz_automatic_code\\day4\\access.log' | |||
a | open for writing, appending to the end of the file if it exists 打开文件等待写入,如果文件已经存在,则在魔剑末尾写入 | ✘ | ✔ | |
b | binary mode 二进制模式 | |||
t | text mode (default) 文本模式,默认为t模式 | |||
+ | open a disk file for updating (reading and writing) 打开一个磁盘文件等待更新(可读写)但是读的 | ✔ | ✔ | |
U | universal newline mode (deprecated) 不赞成使用这一种,在python3中将不再支持 | |||
三、文件操作方法
1.open('文件路径','模式','encoding') 1)如果文件不再当前路径,要把文件的绝对路径写在此处 注意路径分隔符与文件夹的首字母包含 \b 或\n,要么多加一个\进行转移,要么在‘文件路径’的前面 加一个r 转义: 加r: 2)返回值 open() returns a file object whose type depends on the mode, open返回的文件对象基于模式 and through which the standard file operations such as reading and writing are performed. 并且通过模式执行标准文件操作,如执行读或者写操作 When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. 当打开文件是文本模式,将范围文本IO包 When used to open a file in a binary mode, the returned class varies: in read binary mode, it returns a BufferedReader; 当以二进制模式打开文件,如果是读二进制模式,返回BufferedReader; in write binary and append binary modes, it returns a BufferedWriter, and in read/write mode, it returns a BufferedRandom. 如果是写二进制模式,返回BufferedWriter;如果是读写二进制模式,返回BufferedRandom; 3) 编码格式 encoding = ‘xxx’ encoding = ‘utf-8’ #windows中文操作系统,编码使用utf-8,其他应该也可以试试 win下不加编码会标错UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 50: illegal multibyte sequence 4)打开文件python 3只有open,当前目录不用路径.python 2有open和file2个方法 5)若打开的txt编码为ANSI也会报错,错误信息如下: 运行错误 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbc in position 0: invalid start byte 原因txt的编码格式ANSI 2.read() 1)读取整个文件内容 (大文件肯定会慢,这个我还没尝试) 2)读完以后,文件指针将返回到最后一行 3)返回的是字符串类型 3.readline() 1)按行读取文件,返回字符串类型 str,默认是先读第一行 2)若第一行是个空行,将返回空字符串 4.readlines() 1)读取文件中的所有行,以列表list类型返回,行记录即list元素 2)执行一次readlines()文件指针指向文件末尾 5.seek(0) #定位到文件首行 e.g f.seek(0) #也可以使用参数替换0,更灵活 6.tell() #记录当前行位置 e.g point = f.tell() #将f文件当前行的位置赋给point,则下次循环可以从point开始,不一定要从头开始 7.close() #关闭文件 8.write()#写文件 9.writelines()#写入多行 10.flush() #文件刷新 11.truncate() #清空文件 如,f.truncate() |
def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True): # known special case of open """ Open file and return a stream. Raise IOError upon failure. file is either a text or byte string giving the name (and the path if the file isn't in the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.) mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for creating and writing to a new file, and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are: ========= =============================================================== Character Meaning --------- --------------------------------------------------------------- 'r' open for reading (default) 'w' open for writing, truncating the file first 'x' create a new file and open it for writing 'a' open for writing, appending to the end of the file if it exists 'b' binary mode 't' text mode (default) '+' open a disk file for updating (reading and writing) 'U' universal newline mode (deprecated) ========= =============================================================== The default mode is 'rt' (open for reading text). For binary random access, the mode 'w+b' opens and truncates the file to 0 bytes, while 'r+b' opens the file without truncation. The 'x' mode implies 'w' and raises an `FileExistsError` if the file already exists. Python distinguishes between files opened in binary and text modes, even when the underlying operating system doesn't. Files opened in binary mode (appending 'b' to the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is appended to the mode argument), the contents of the file are returned as strings, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given. 'U' mode is deprecated and will raise an exception in future versions of Python. It has no effect in Python 3. Use newline to control universal newlines mode. buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows: * Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device's "block size" and falling back on `io.DEFAULT_BUFFER_SIZE`. On many systems, the buffer will typically be 4096 or 8192 bytes long. * "Interactive" text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files. encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent, but any encoding supported by Python can be passed. See the codecs module for the list of supported encodings. errors is an optional string that specifies how encoding errors are to be handled---this argument should not be used in binary mode. Pass 'strict' to raise a ValueError exception if there is an encoding error (the default of None has the same effect), or pass 'ignore' to ignore errors. (Note that ignoring encoding errors can lead to data loss.) See the documentation for codecs.register or run 'help(codecs.Codec)' for a list of the permitted encoding error strings. newline controls how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows: * On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. If closefd is False, the underlying file descriptor will be kept open when the file is closed. This does not work when a file name is given and must be True in that case. A custom opener can be used by passing a callable as *opener*. The underlying file descriptor for the file object is then obtained by calling *opener* with (*file*, *flags*). *opener* must return an open file descriptor (passing os.open as *opener* results in functionality similar to passing None). open() returns a file object whose type depends on the mode, and through which the standard file operations such as reading and writing are performed. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open a file in a binary mode, the returned class varies: in read binary mode, it returns a BufferedReader; in write binary and append binary modes, it returns a BufferedWriter, and in read/write mode, it returns a BufferedRandom. It is also possible to use a string or bytearray as a file for both reading and writing. For strings StringIO can be used like a file opened in a text mode, and for bytes a BytesIO can be used like a file opened in a binary mode. """ pass
|
四、疑问
1.f.tell()返回的位置在内存中的值吗? |