In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to read and write documents in Python". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to read and write files in Python"!
CharacterMeaning'r'open for reading (default) 'w'open for writing, truncating the file first'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 (for backwards compatibility Should not be used in new code) mode describes rt reading text, default mode rb reads binary data wt writes text wb writes binary r + does not empty the original file, reads and writes w + empties the original file, and reads and writes a + reads and writes at the end of the file
First, create a new "abc.txt" file on the left. The contents of the file are as follows:
I
Love
CSDN
Read-only mode (default mode)
> f=open ("C:/Users/Administrator/Desktop/abc.txt", "r") > print (f.read ()) IloveCSDN > f.close ()
Write mode
> f=open ("C:/Users/Administrator/Desktop/abc.txt", "w") > f.write ("test") > f.close ()
The result of the output is:
Test
When using "w" mode, python will overwrite the original file and form a new one. Note here that if the written file does not exist, python will automatically create a new file.
Additional mode
> f=open ("C:/Users/Administrator/Desktop/abc.txt", "a") > f.write ("test") > f.close ()
The result of the output is:
I
Love
CSDNtest
In addition, we can also set how to read and write:
To read in binary mode:
> f=open ("C:/Users/Administrator/Desktop/abc.txt", "rb") > print (f.read ()) > f.close () bounded I\ r\ nlove\ r\ nCSDN'
One of the great uses of binary reading is to save matplotlib's interactive picture page:
Save the interactive picture page
Import matplotlib.pyplot as pltimport pickle as pl# calls matplotlib's figure object fig = plt.figure () x = [1pje 2je 3je 4je 5] y = [1pje 2je 3je 4je 5] plt.plot (xmemy) # serializes the figure object and saves the pl.dump (fig,open ('C GunpUserUserUserandUserandDesktopcharpfig.pickleChargedjigwb'))
Read the interactive page:
Import matplotlib.pyplot as pltimport pickle as pl# loads the serialized file fig = pl.load (open ('open) plt.show () # get the picture information print (fig.axes [0] .lines [0] .get _ file ()) so far, I believe you have a deeper understanding of "how to read and write files in Python", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.