In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Python how to read and write documents, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Reading and writing files from files is a common requirement of any programming language. Any file needs to be opened before it can be read or written. Most programming languages use the open () method to open files for reading and writing using file objects (file object). You can use different types of file access modes as parameters of the open () method to indicate the purpose of opening the file. This parameter is optional. The close () method is used to release the resources occupied by the file object after the file operation is completed. Python programming can handle two types of files. They are text files and binary files.
File access mode:
As mentioned earlier, different types of file access patterns can be used in the open () method, which is described in this section. The common patterns are described below.
The pattern usage t represents a text file, which is the default file type. B represents a binary file. R Open a file for reading, which is the default mode for opening any file. W open the file for writing. If X does not exist, it will open the file for writing. An if the file exists, it opens the file and adds content at the end of the file; otherwise, create the file and add content at the beginning of the file. R + open the file for reading and writing, and place the cursor at the beginning of the file. If the file does not exist, an error is raised. W + opens the file for reading and writing, and overwrites the data if the file already exists. A + opens the file for reading and writing and places the cursor at the end of the existing file. If the file does not exist, it creates the file.
Methods:
There are many ways to read or write files in Python. This article details the most commonly used methods.
Open ():
This method contains two parameters. The first parameter is mandatory and is used to get the file name for reading or writing. The second parameter is optional and sets the file access mode. The default file access mode is rt. The return type of this method is a file object that is used to read and write files.
Syntax:
FileObject = open ("Filename", "FileMode")
Close ():
This method is used to close the file and make it available for other purposes. After this method is called, the file processing object will not be available.
Read ():
This method is used to read a specific number of bytes from a file using a file object.
Readline ():
This method is used to read specific lines from a file using a file object.
Readlines ():
This method is used to read all lines of files separated by commas (,) using file objects.
Write ():
This method is used to write content to a file using a file object.
Read the text file:
Create a text file called "linuxmi.txt" that contains the following for use in the next section of this article.
Linux fans
Www.linuxmi.com
Linuxmi.com
Linuxmi
M.linuxmi.com
Example 1: use read (), readline (), and readlines () to read the file
Use the following Python script to create a file named linuxmi1.py. It uses read () to read the file based on byte size, readline () to read a fixed number of characters from the file, and readlines () to read all the lines of the file in the array.
# Open file for reading FileHandler = open ("linuxmi.txt", "r") # read file contents according to size print ('output from read () method\ nFileHandler.read (2048)) # close file FileHandler.close () # Open file for reading and writing FileHandler = open ("linuxmi.txt") "r +") # read the file contents of the third line print ('output from the readline () method\ nFileHandler.readline (5)) # close the file FileHandler.close () # Open the file for reading and append FileHandler = open ("linuxmi.txt", "r") # Open the file for reading and append print (' output from the readlines () method\ n' FileHandler.readlines () # close the file FileHandler.close ()
Output:
After running the script, the following output is displayed.
Example 2: use a loop to read the file line by line
Use the following script to create a file named linuxmi2.py. It will use the for loop to read (fileObject) from the file object and print each line of the file.
# Open the file to read fileObject = open ("linuxmi.txt", "r") # read the file line by line and print for line in fileObject: print (line) in the terminal
Output:
After running the script, the following output is displayed.
Example 3: read the file by using the with statement
Use the following script to create a file named linuxmi3.py. It will read the file through statements without using any file objects.
# read the file with open ("linuxmi.txt") as fhandler: print (fhandler.readlines ()) using the with statement
Output:
After running the script, the following output is displayed.
Write a text file:
You can write content to a file by defining a file object or using it with a statement.
Example 4: write to a file using a file object (file object)
Use the following script to create a file named linuxmi.com.py. It opens a text file for writing and writes three lines using the write () method.
# Open the file to write fileObject = open ("www.linuxmi.com.txt", "w") # add some text fileObject.write ("Linux fans welcome you\ n") fileObject.write ("our URL is www.linuxmi.com\ n") fileObject.write ("mobile station m.linuxmi.com\ n") # close the file fileObject.close ()
Output:
Run the script and check to see if the file was created with the content. After running the script and running the "cat" command, the following output is displayed.
Example 5: write a file using the with statement
You can write content to a file without defining a file object. Use the following script to create a file named m.linuxmi.com.py. By using the with statement, it will write two lines in the file.
# use the with statement to open the file to write with open ("m.linuxmi.com.txt",'w') as fileObj: fileObj.write ("Linux fans provide you with the latest open source information\ n") fileObj.write ("linuxmi.com provides the latest and most cutting-edge open source technology\ n") does it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.