python读写文件_Python读写文件

旧城等待, 2022-12-07 13:28 481阅读 0赞

93f0cb76b7c3483ccd2afe7b37d9ce04.png

python读写文件

In this tutorial, you’ll learn how to read and write file in Python.

在本教程中,您将学习如何在Python中读取和写入文件。

Program to Write to File

程序写入文件

  1. f = open("newtext.txt","w")
  2. f.write("something to be write")
  3. f.close()

Output:

输出:

this program will create a new file (if not exist) named as “newtext.txt” in the same directory where our program is located and write a line “something to be write”.

该程序将在我们程序所在的目录中创建一个名为“ newtext.txt”的新文件(如果不存在),并写一行“要写的东西”。

Program to Read File

程序读取文件

  1. f = open("newtext.txt","r")
  2. print f.read()
  3. f.close()

Output:

输出:

something to be write

要写的东西

As we have seen the program, it is very easy to read and write into a file in python. In python we can work with two type of files Text files and Binary files. In this tutorial we are working with text file.

如我们所见,该程序非常容易在python中读写文件。 在python中,我们可以使用两种类型的文件:文本文件和二进制文件。 在本教程中,我们将处理文本文件。

Python读写文件 (Python Read and Write File)

Let’s understand above programs.

让我们了解以上程序。

  1. f = open("newtext.txt", "w")

open() is just a function to get the file object. It gets the file object and returns it to the f, we can name f as we want. using file object’s methods and attributes we can access the information that file have.

open()只是获取文件对象的函数。 它获取文件对象并将其返回给f ,我们可以根据需要命名f 。 使用文件对象的方法和属性,我们可以访问文件所包含的信息。

open() takes two arguments, first the file name (in our case, its “newtext.txt”) and second one is for access mode (optional).

open()有两个参数,第一个是文件名(在本例中为“ newtext.txt”),第二个是用于访问模式(可选)。

Access Modes: it is an attribute of file object. Which tells, in which mode the file is opened.

访问模式:它是文件对象的属性。 哪个表明以哪种模式打开文件。

There are 4 types of access modes.

有4种访问模式。

  1. r: r stands for reading mode. If we open our file in this mode then we can only read the file’s information.

    r: r代表阅读模式。 如果以这种模式打开文件,则只能读取文件的信息。

  2. w: w stands for writing mode. If we open our file in this mode then we can only write into the file, can’t read. If the file is not already exist then it will create new one, if it exists then it will delete all the previous content from the file and start writing from starting of the file.

    w: w代表书写模式。 如果以这种模式打开文件,则只能写入文件,无法读取。 如果该文件尚不存在,则将创建一个新文件;如果该文件存在,则将从文件中删除所有先前的内容,并从文件开头开始写入。

  3. a: a stands for appending mode. It is same as writing mode but the difference is it will not delete the previous content of the file. It starts writing from the last of the file. In other words, it will append (concatenate) the new content with previous one.

    a: a表示追加模式。 它与写入模式相同,但区别在于它不会删除文件的先前内容。 它从文件的最后开始写入。 换句话说,它将新内容附加(连接)到先前的内容。

  4. r+: special read and write mode. If we open the file using this mode then we can both read and write into the file.

    r +:特殊的读写模式。 如果使用此模式打开文件,则可以读取和写入文件。

Note: access mode is optional in open() method. By default the file will open into read mode (r).

注意:在open()方法中,访问模式是可选的。 默认情况下,文件将进入读取模式(r)。

Come to the next line of our program.

进入我们程序的下一行。

  1. f.write("something to be write")

write() is a method of file object to write into the file. We will write in the parenthesis, what we want to write into the file (as shown above).

write()是文件对象写入文件的方法。 我们将在括号中写入要写入文件的内容(如上所示)。

To read from the file:

要读取文件:

  1. print f.read()

read() is a method of file object to read the information stored in a file object. read() method read all the information from the given file object and returns it.

read()是文件对象读取存储在文件对象中的信息的方法。 read()方法从给定的文件对象中读取所有信息,并将其返回。

Last line in our program:

我们程序的最后一行:

  1. f.close()

close() is a method of file object to close the opened file. Which means we can’t access the file in our program now.

close()是文件对象关闭打开文件的方法。 这意味着我们现在无法在程序中访问文件。

There are many other useful methods of the file objects like:-

文件对象还有许多其他有用的方法,例如:

read(int): if we pass any digit to the read() method then it will return same number of characters from the file object.

read(int):如果我们将任何数字传递给read()方法,则它将从文件对象返回相同数量的字符。

Example:

例:

Let’s say we have a text file named as “newtext.txt” and “the crazy programmer” have written into it.

假设我们有一个名为“ newtext.txt”的文本文件,并且已写入“疯狂的程序员”。

  1. f = open("newtext.txt", "r")
  2. print f.read(9)
  3. f.close()

Output:

输出:

the crazy

疯子

readline(): it is used to read a single line (separated by enter key or \n ) from the file object.

readline():用于从文件对象中读取一行(由回车键或\ n分隔)。

Example:

例:

Lets say our text file is look like this:

可以说我们的文本文件如下所示:

line number 1 line number 2 line number 3

行号1 行号2 行号3

  1. f = open("newtext.txt", "r")
  2. print f.readline()
  3. f.close()

Output:

输出:

line number 1

行号1

  1. f = open("newtext.txt", "r")
  2. print f.readline()
  3. print f.readline()
  4. f.close()

Output:

输出:

line number 1 line number 2

行号1 行号2

readlines(): it reads all lines in the file object and return it as a List of lines.

readlines():读取文件对象中的所有行,并将其作为行列表返回。

Example:

例:

  1. f = open("newtext.txt", "r")
  2. print f.readlines()
  3. f.close()

Output:

输出:

[ ‘line number 1\n’ , ‘line number 2\n’ , ‘line number 3\n’]

[‘行号1 \ n’,’行号2 \ n’,’行号3 \ n’]

If we want to print a specific line then:

如果我们要打印特定的行,则:

  1. f = open("newtext.txt", "r")
  2. lines = f.readlines()
  3. #to print the 3rd line
  4. print lines[2]
  5. f.close()

Output:

输出:

line number 3

行号3

seek(int): it sets the position of reading and writing pointer. Using seek() method we can navigate to start or last of the file object.

seek(int):设置读写指针的位置。 使用seek()方法,我们可以导航到文件对象的开始或末尾。

This method takes 0,1,2 as argument.

此方法以0,1,2作为参数。

  • 0 – takes pointer to the starting

    0 –将指针指向起点

  • 1 – current position of the pointer

    1 –指针的当前位置

  • 2 – takes pointer to the end

    2 –将指针指向末尾

Example:

例:

  1. f = open("newtext.txt", "r")
  2. print f.readline()
  3. print f.readline()
  4. f.seek(0)
  5. print f.readline()
  6. f.close()

Output:

输出:

line number 1 line number 2 line number 1

行号1 行号2 行号1

We can also use with statement to open the files for better indentation.

我们还可以使用with语句打开文件以获得更好的缩进。

  1. #to write into the file
  2. with open("file.txt","w") as f:
  3. f.write("example line")

It will create a new file named as “file.txt” and write “example line” into it.

它将创建一个名为“ file.txt”的新文件,并将“示例行”写入其中。

Comment below if you have queries regarding above python file handling tutorial.

如果您对以上python文件处理教程有疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/01/python-read-write-file.html

python读写文件

发表评论

表情:
评论列表 (有 0 条评论,481人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python 文件

    常用的读写模式的类型有: w 以覆盖的方式打开 a 以追加的方式打开 r+ 以读写模式打开 W+ 以读写模式打开 a+ 以读写模式打开 rb 以二进制读写模

    相关 Python文件

    Python读写文件 1.open 使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。 fi