How to read and write files in Python

File Access Mode:

As already mentioned, open () method may use different types of file access mode, which will be described in this section. Common mode as follows.

mode use
t It represents a text file is the default file type.
b Represent a binary file.
r Open the file for reading, which is the default mode for any open file.
w Open the file for writing.
x If not, it will open the file for writing.
a If the file exists, it will open the file and add the contents of the end of the file; otherwise, create a file and add content to the beginning of the file.
r+ Open file for reading and writing, and the cursor at the start of the file. If the file does not exist, an error is thrown.
w+ Open file for reading and writing, if the file already exists, the data is overwritten.
a+ Open file for reading and writing, and the cursor is placed at the end of the existing file file. If the file does not exist, it will create the file.

method:

Many methods exist to read or write the file in Python. The most commonly used method detailed herein.

open():

This method includes two parameters. The first parameter is mandatory for acquiring the file name to read or write. The second parameter is optional, is used to set the file access mode. The default file access mode is "rt". The return type of the method is a file object, for reading and writing files.

grammar:

FileObject = open(“Filename”,”FileMode”)
close():

This method is used to close the file and make it available for other purposes. After calling this method, the file to be processed will not be available.

read():

This method is used for reading a specific file objects number of bytes from the file.

readline():

This method for using the file object file read from a specific line.

readlines():

This method for reading a file object by using the comma (,) files all lines separated.

write():

This method is used to use the file object will write the contents of the file.

Read a text file:

Example 1: Use read (), readline () and the readlines () to read the file


# 打开文件进行读取
FileHandler = open("linuxidc.txt","r")
# 根据大小读取文件内容
print('输出来自 read() 方法\n',FileHandler.read(2048))
# 关闭文件
FileHandler.close()
# 打开文件进行读写
FileHandler = open("linuxidc.txt","r+")
# 读取第三行的文件内容
print('输出来自 readline() 方法\n',FileHandler.readline(5))
# 关闭文件
FileHandler.close()
# 打开文件进行读取和附加
FileHandler = open("linuxidc.txt","r")
# 打开文件进行读取和附加
print('输出来自 readlines() 方法\n',FileHandler.readlines())
# 关闭文件
FileHandler.close()

Output:

After running the script, the following output.

How to read and write files in Python

Example 2: using a loop to read the file line by line


# 打开文件进行读取
fileObject = open("linuxidc.txt", "r")
# 逐行读取文件并在终端中打印
for line in fileObject:
  print(line)

Output:

After running the script, the following output.

How to read and write files in Python

 

Example 3: to read the file by using the with statement


# 使用with语句读取文件
with open("linuxidc.txt") as fhandler:
  print(fhandler.readlines())

Output:

After running the script, the following output.

How to read and write files in Python

Writing text file:

Or used in conjunction with the file object defined by the statement, the content may be written to the file.

Example 4: Use write file object file (file object)


# 打开文件进行写入
fileObject = open("www.linuxidc.com.txt", "w")
#  添加一些文本
fileObject.write("Linux公社欢迎您\n")
fileObject.write("我们的网址是www.linuxidc.com\n")
fileObject.write("手机站m.linuxidc.com\n")
# 关闭文件
fileObject.close()

Output:

Run the script, and checks if the file content creation. After running the script and run "cat" command, the following output.

How to read and write files in Python

Example 5: Use file written statements with


# 使用with语句打开文件进行写入
with open("m.linuxidc.com.txt",'w') as fileObj:
   fileObj.write("Linux公社为您提供最新的开源资讯\n")
   fileObj.write("linuxidc.com提供最新最前沿的开源技术\n")

Output:

After running the script and "cat" command to read the file, the following output.

How to read and write files in Python

to sum up

This conventional method of reading tutorial content and write to the file from the file by using a very simple example is described. Python novice will be able to know the use of reading or writing files needed to function. Have you learned it?

Guess you like

Origin www.linuxidc.com/Linux/2020-03/162719.htm