Python file operations

Source: Internet
Author: User

First, we need to know the concept that an application cannot operate directly on computer hardware.

In the operating system, the operating system provides the file system externally, the files on the hard disk are managed by the file system,

Read-write hard disk is a kind of hardware operation, so we want to do file operation, we must through the file system this interface for file operation

So

我们要想进行文件读写等操作,就必须先向操作系统发起系统调用,由操作系统的内核来进行文件的读写操作,操作系统把执行结果返回给应用程序,最后则应用程序把执行结果呈现在用户面前

Python provides a system call method for file read and write operations:open()

open()是python内置函数,我们可以通过调用open()函数来向操作系统发起系统调用,进行文件的读写操作
The use of the open () function

Create a new open_func.py file, and then create a new File1.txt file

The contents of the File1.txt file are:

离离原上草,一岁一枯荣。野火烧不尽,春风吹又生。

Open File1.txt in open_func.py file first

>>> f=open("file1.txt")     # 没有指定file1.txt文件的绝对路径,所以这里用的是相对路径                            # 第二个参数为读取文件的模式,不写默认为"r",即读取模式                            # 把打开文件的句柄赋值给一个变量f>>> print(f)<_io.TextIOWrapper name=‘file1.txt‘ mode=‘r‘ encoding=‘UTF-8‘>>>> f.read()                # f.read()会读取指定文件的指定长度字符的内容,如果没有指定读取长度,则读取所有内容‘离离原上草,\n一岁一枯荣。\n野火烧不尽,\n春风吹又生。\n‘>>> f.read()                # 再次调用f.read()方法,其打印结果为空,也说明上一次f.read()读取指定文件的所有内容‘‘>>> f.close()               # 关闭文件

Points to note:

    • Open a file after the operation is completed must close this file, otherwise the open file will always exist in the system memory, occupy the system resources
Specify the read length method: F.read ()
[[email protected] ~]# cat file2.txt       # 在系统命令提示符下查看文件file2.txt的内容pythonlinuxmysql>>> f=open("file2.txt")             # 以默认方式打开一个不存在的文件file2.txt,会抛出异常Traceback (most recent call last):  File "<stdin>", line 1, in <module>FileNotFoundError: [Errno 2] No such file or directory: ‘file2.txt‘

Working in the Python interpreter

>>> f=open("file2.txt","r")         # 以读模式打开文件>>> f.read(2)                       # 读取两个字符‘py‘>>> f.read(5)                       # 再向后读取5个字符‘thon\n‘>>> f.read(5)                       # 再次继续读取5个字符‘linux‘>>> f.close()                       # 关闭文件
How to read one line at a time: F.readline ()
>>> f=open("file1.txt")     # 再次打开file1.txt文件>>> f.readline()            # f.readline()方法执行一次读取文件的一行内容‘离离原上草,\n‘>>> f.readline()            # 再次执行f.readline()方法,会接着光标在文件中的内容再次读取文件的一行内容‘一岁一枯荣。\n‘>>> print(f.readline(),end="")      # 这次使用print()方法打印f.readline()方法读取的一行内容,并指定文件结尾野火烧不尽,>>> print(f.readline(),end="")春风吹又生。>>> f.readline()            # 文件已经被读取完了,再次调用f.readline()方法读取文件内容会返回空‘‘>>> f.close()               # 因为文件已经被读取完,光标已经被移动到文件的末尾了,所以关闭文件
How to read the entire file content Generation list: F.readlines ()
>>> f=open("file1.txt")     # 打开文件>>> f.readlines()           # f.readlines()会一次性读取文件的所有内容,并把每一行做为一个元素放入一个列表中[‘离离原上草,\n‘, ‘一岁一枯荣。\n‘, ‘野火烧不尽,\n‘, ‘春风吹又生。\n‘]>>> f.write("aaa")          # 向文件中写入一行"aaa",因为是以读的方式打开文件,所以调用f.write()方法向文件中写入一行是会提示错误:无法写入Traceback (most recent call last):  File "<stdin>", line 1, in <module>io.UnsupportedOperation: not writable>>> f.close()
Now open the file in "W" mode
>>> f=open("file1.txt","w")     # 以"w"的方式打开文件>>> f.close()                   # 关闭文件重新打开一个命令窗口,查看file1.txt文件的内容

In the previous example, there is content in the File1.txt file, but viewing the contents of the file here is empty.

[[email protected] ~]# cat file1.txt       # 可以看到文件中没有任何内容[[email protected] ~]# ll file1.txt        # 文件大小变为0了-rw-r--r-- 1 root root 0 Jan 27 22:46 file1.txt

In the example above, we can see that the original contents of the specified file are cleared when the file is opened in "W" mode.

实际上,在python中,以"r"方式打开文件时,如果被打开的文件不存在时,是会抛出异常的而以"w"模式打开文件时,如果文件不存在则新建这个文件,如果文件已经存在,则清空这个文件的原有内容

Let's look at another example.

>>> f=open("file1.txt","w")         # 以"w"模式打开file1.txt>>> f.write("hello world")          # 向文件中写入一行内容11>>> f.write("hello python")         # 向文件中再次写入一行内容12>>> f.close()                       # 关闭文件

View the contents and size of the File1.txt file on the system command line

[[email protected] ~]# cat file1.txt       # 查看文件内容hello worldhello python[[email protected] ~]# ll file1.txt-rw-r--r-- 1 root root 23 Jan 27 22:59 file1.txt

Viewing the contents of a file you can see that there are no line breaks in the file, so when writing to a file, we need to add line breaks at the end

>>> f=open("file1.txt","w")         # 以"w"模式打开file1.txt>>> f.write("hello python\n")       # 向文件中添加一行,在行尾添加换行符13      >>> f.write("hello world\n")        # 再次向文件中添加一行,在行尾也添加换行衔12>>> f.close()                       # 关闭文件

View the contents and size of the file again at the system command prompt

[[email protected] ~]# cat file1.txt       # 查看文件内容,可以看到现在已经可以正常显示了hello python    hello world[[email protected] ~]# ll file1.txt        # 查看文件大小-rw-r--r-- 1 root root 25 Jan 27 23:03 file1.txt

When the file is read, F.readlines reads all the contents of the file one time and adds each line as an element to the list
There is also a F.writelines method when writing to a file.

method to write a list of string elements to a file: F.writelines ()

The F.writelines () method can write a list of elements as rows to a file

It is important to note that:列表中的元素必须是字符串类型

>>> f=open("file1.txt","w")>>> f.writelines(["python\n","linux\n","mysql\n"])      # 调用f.writelines()方法把一个列表写入到一个文件中>>> f.close()

To view the contents and size of a file in a System command Prompt window

[[email protected] ~]# cat file1.txt       # 查看文件内容pythonlinuxmysql[[email protected] ~]# ll file1.txt-rw-r--r-- 1 root root 19 Jan 27 23:09 file1.txt

You can see that the list of string elements has been written to the file.

Open and operate files in "a" mode

When opening a file in "a" mode, if the specified file does not exist, create the specified file
If the specified file already exists, move the cursor to the end of the file and add the contents to the last line of the file

>>> f=open("file1.txt","a")     # 以"a"模式打开文件>>> f.write("nginx\n")          # 向文件末尾追加一行内容6>>> f.write("javascript\n")     # 向文件末尾再次追加一行内容11>>> f.close()                   # 关闭文件

To view the contents and size of a file in a System command Prompt window

[[email protected] ~]# cat file1.txt   # 可以看到文件file1.txt的末尾已经添加了两行内容pythonlinuxmysqlnginxjavascript[[email protected] ~]# ll file1.txt    # 文件大小发生改变-rw-r--r-- 1 root root 36 Jan 27 23:16 file1.txt

Note the point:

在向文件中添加内容时,实际上是在内存中向文件添加内容的,只有等到文件被关闭的时候,解释器才会把内存中被修改的文件内容同步到文件中,在这个期间是有风险的,比如在文件未被保存到硬盘上之前,主机突然断电,那么这个文件的内容就有可以被损坏

You can then use the F.flush () method to forcibly save the contents of the modified file in the memory to the hard disk.

How to manually save a file in memory to your hard disk: F.flush ()
>>> f=open("file1.txt","a")         # 以追加模式打开文件>>> f.write("mongodb\n")            # 向文件中添加一行内容8>>> f.write("openstack\n")          # 再次向文件中添加一行内容10>>> f.flush()                       # 调用f.flush()方法,把内存中的文件内容保存到硬盘中

To view the contents and size of a file in a System command Prompt window

[[email protected] ~]# cat file1.txt       # 在前面,并没有关闭文件,在这里还是可以看到添加后的文件内容pythonlinuxmysqlnginxjavascriptmongodbopenstack[[email protected] ~]# ll file1.txt        # 文件大小发生改变-rw-r--r-- 1 root root 54 Jan 28 10:57 file1.txt
F.closed to see if the file is closed, returns a Boolean value

This example follows the example above.

>>> f.closed        # 在上面的例子里,调用f.flush()方法把内存中的文件内容保存到硬盘上,并没有关闭文件,查看文件是否关闭,返回False,说明文件没有关闭False>>> f.close()       # 关闭文件>>> f.closed        # 再次查看文件是否关闭,返回True说明文件已经关闭了True
To move the cursor to the location specified in the file: F.seek (Offset[,whence])

Parameter description:

第一个参数为移动的字节数,第二个参数表示从哪个位置开始移动,0表示以文件开始为参照点1表示以当前位置为参数点2表示以文件末尾位置为参照点

Example:

>>> f=open("file1.txt","rb")        # 在这里必须以"b"模式打开文件,否则会抛出异常>>> f.seek(5)                       # 当前光标在文件首部,把光标从文件首部向后移动5个字节5>>> f.seek(5,1)                     # 再以光标当前所有位置为参照,向后移动五个字节10>>> f.tell()                        # 查看当前光标所在位置10>>> f.seek(3,1)                     # 以光标当前所在位置为参照,向后移动3个字节13>>> f.tell()                        # 查看当前光标所有位置13>>> f.seek(-2,2)                    # 把光标移动到文件末尾,然后再向前移动2个字节52>>> f.read()                        # 读取光标所在处到文件末尾的所有内容b‘k\n‘>>> f.tell()                        # 查看光标所在位置54>>> f.close()
To see how the current cursor is located: F.tell ()
f.tell()方法的位置为字节,返回值为数字

Example:

>>> f=open("file1.txt","r")>>> f.read()‘python\nlinux\nmysql\nnginx\njavascript\nmongodb\nopenstack\n‘>>> f.seek(10)10>>> f.tell()10>>> f.read()‘ux\nmysql\nnginx\njavascript\nmongodb\nopenstack\n‘>>> f.tell()54
Takes the cursor position as a reference, intercepts the specified length of content, and the remainder is deleted: F.truncate ()

Example:

>>> f=open("file2.txt","w")         # 以写模式打开文件>>> f.write("1111111\n")            # 向文件中写入一行内容8>>> f.write("2222222\n")            # 向文件中写入第二行内容8>>> f.write("3333333\n")            # 向文件中写入第三行内容8>>> f.truncate(5)                   # 从文件开始位置向后截取5个字节的长度的内容5>>> f.close()

Viewing the contents of a file at a system command prompt

[[email protected] ~]# cat file2.txt11111[[email protected] ~]#
The rest of the usage
f.name              # 获取被打开文件的文件名f.encoding          # 获取文件的被打开的编码方式f.readable()        # 判断文件当前是否可读f.writable()        # 判断文件当前是否可写

Example:

>>> f=open("file1.txt","r")         # 以读模式打开文件file1.txt>>> f.name                          # 查看文件名‘file1.txt‘     >>> f.encoding                      # 查看文件以哪种编码方式被打开‘UTF-8‘>>> f.readable()                    # 以读模式打开此文件,所以判断文件是否可读,返回值为TrueTrue>>> f.writable()                    # 以读模式打开此文件,所以判断文件是否可写,返回值为FalseFalse   >>> f.close()                       # 关闭文件>>> f=open("file2.txt","w")         # 以写模式打开文件file2.txt>>> f.name‘file2.txt‘>>> f.encoding‘UTF-8‘>>> f.readable()                    # 以写模式打开文件,所以在这里判断文件是否可读时,返回FalseFalse>>> f.writable()                    # 以写模式打开文件,判断文件可写返回值为TrueTrue>>> f.close()

In Python, in addition to the above-mentioned three ways to open files, there is another way to open the file, which is to open the file in binary mode, that is, "RB", "WB", "AB"

Another file file2.txt is available with the content

离离原上草,一岁一枯荣。野火烧不尽,春风吹又生。

Open a file in a "rb" mode to file2.txt see what it reads

>>> F=open ("File2.txt", "RB") # Open file in read mode file2.txt>>> f.read () # When viewing the contents of a file, you can see that it is the bytes grid Type B ' \xe7\xa6\xbb\xe7\xa6\xbb\xe5\x8e\x9f\xe4\xb8\x8a\xe8\x8d\x89\xef\xbc\x8c\n\xe4\xb8\x80\xe5\xb2\x81\xe4\xb8 \x80\xe6\x9e\xaf\xe8\x8d\xa3\xe3\x80\x82\n\xe9\x87\x8e\xe7\x81\xab\xe7\x83\xa7\xe4\xb8\x8d\xe5\xb0\xbd\xef\xbc \x8c\n\xe6\x98\xa5\xe9\xa3\x8e\xe5\x90\xb9\xe5\x8f\x88\xe7\x94\x9f\xe3\x80\x82\n ' >>> f=open ("File2.txt "," RB ") >>> F.read (). Decode (" UTF8 ") # because writing is written in" UTF8 "encoded format, so here to decode" UTF8 "in the format of ' off the grass, \ n One year old a withered flourish. \ n The wild Fire is endless, the spring breeze blows and is born. \ n ' >>> f=open ("File3.txt", "WB") # Open the file in write mode file3.txt>>> f.write ("Python") # now writing to the F object will throw  Exception: The written content needs to be a bytes object and cannot be a string format traceback (most recent calls last): File ' <stdin> ', line 1, in <module>typeerror: A Bytes-like object is required, not ' str ' >>> f.write ("python\n". Encode ("UTF8")) # encoded in ' UTF8 ' format, then written 7>> > f.write ("Hello \ n". Encode ("UTF8")) 10>>> F.close ()

viewing file size and content at a system command prompt

[[email protected] ~]# cat file3.txtpython你好呀[[email protected] ~]# ll file3.txt-rw-r--r-- 1 root root 17 Jan 28 16:41 file3.txt

Benefits of opening files in "B" mode:

1.读取文件的时候就是二进制格式的,不容易产生乱码2.处理非文本类型的文件时,写入文件的时候就需要以二制格式写入,否则会抛出异常

Example:

用python的二进制方法把一个张图片aa.jpg复制一份bb.jpgread_file=open(‘aa.jpg‘,‘rb‘)write_file=open("bb.jpg","wb")write_file.write(read_file.read())
[[email protected] ~]# ll *.jpg-rw-r--r-- 1 root root 95406 Jan 19 08:38 aa.jpg-rw-r--r-- 1 root root 95406 Jan 28 17:15 bb.jpg

Summarize:

打开文件时,需要指定文件路径和打开方式。打开文件后,即可获取指定文件的文件句柄,然后通过操作文件句柄来操作指定文件        打开文件的模式有:    r       只读模式,打开文件默认模式,文件必须存在,不存在则抛出异常    w       只写模式,文件不存在则创建,存在则清空原有内容    a       追加模式,可以读取文件,文件不存在则创建,存在则向文件追加内容“+”表示可以同时读写某个文件r+      读写(可读可写)w+      写读(可读可写)a+      写读(可读可写)"b"表示以字节的方式操作rbwbab以b方式打开文件时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码

Small Demo:

Replace the contents of the file with the Python method

View Initial file contents

[[email protected] ~]# cat test.txt aaaapythonaaaapythonaabbbblinuxbbblinuxbbbcccmysqlcccmysqlcccdddnginxdddnginxddd

Now I want to hit all the Nginx in the Test.txt file and replace it with httpd.

The script is as follows:

import osread_f = open("test.txt", "r")write_f = open(".text.txt.swap", "w")for line in read_f:    if ‘nginx‘ in line:        line = line.replace("nginx", "httpd")    write_f.write(line)read_f.close()write_f.close()os.remove("test.txt")os.rename(".text.txt.swap", "test.txt")

Run this Python script, and then view the contents of the Test.txt

[[email protected] ~]# cat test.txt aaaapythonaaaapythonaabbbblinuxbbblinuxbbbcccmysqlcccmysqlcccdddhttpddddhttpdddd

You will find that the contents of the file Test.txt have been modified.

With context Management

When opening a file with open, it is easy to forget to close the open file, which will be opened by the file has been running in memory, virtually resulting in a waste of system resources

You can then use the with context management to perform file operations to resolve the waste of system resources that are caused by forgetting to close the file

Example of replacing with the content above with refactoring

import oswith open("a.txt","r") as read_f,open(".a.txt.swap","w") as write_f:    for line in read_f:        if "nginx" in line:            line=line.replace("nginx","httpd")        write_f.write(line)os.remove("a.txt")os.rename(".a.txt.swap","a.txt")

You can also complete the same function without manually closing the files that are opened

Python file operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.