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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to manipulate files in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Introduction of documents
What is a file? As shown in the picture:
The purpose of using the file:
Save the data on disk and store some storage so that the program can be used directly the next time it is executed without having to make a new copy, which saves time and effort.
Opening and closing of files 1. Open a file
In python, using the open function, you can open an existing file or create a new file in open (filename, access mode) format:
F = open ('file','w') or f = open ('file','r')
As shown in the following figure: different ways to open a file
Common file operations are: write, read, append
two。 Write data (write)
Format:
Object = open ("file", w) object .write ("write data") object .close ()
Example: open the file by writing and write the data
F=open ('666.txtexamples') f.write ('6666666666') f.close ()
Continue to write data
F=open ('666.txtexamples') f.write ('111111') f.close ()
If the file does not exist, then create it, if it does, empty it first, and then write the data.
3. Read data (read)
Format:
Object = open ("file", r) variable = object .read () print (variable) object .close ()
Example: open the file and read the data by reading
F=open ('666.txt) a=f.read () print (a) f.close ()
When reading data,'r 'can be omitted from writing, for example:
F=open ('666.txt') a=f.read () print (a) f.close ()
If you open a file with open, if you use "r", you can omit it, that is, only write open ('test.txt') if there is no file, open an error, and the file can operate. If there is a Chinese display in the file, there will be garbled codes that need to be added to encoding='utf-8' open (' test.txt', "r", encoding='utf-8').
Read data of a specified length (in bytes)
F=open ('666.txthandjigr') a=f.read (5) # read 5 data print (a) f.close ()
You can read data from a file using read (num). Num represents the length of the data to be read from the file (in bytes). If num is not passed in, it means to read all the data in the file.
4. Read data (readlines)
Readlines is just like when read has no parameters, readlines can read the contents of the whole file at once in the way of lines, and return a list, in which the data of each line is an element
Format:
Object = open ("file", r) variable = object .readlines () print (variable) object .close ()
Example:
F=open ('666.txt) a=f.readlines () print (a) print (type (a))
5. Read data (readline)
Readline can read the contents of the whole file line by line.
Format:
Object = open ("file", r) variable = object .readline () print (variable) object .close ()
Example:
F=open ('666.txt) a=f.readline () a=f.readline () print (' 1:%s'%a) a=f.readline () print ('2:%s'%a) a=f.readline () print (' 3:%s'%a) f.close ()
6. Writable, append (a)
Format:
Object = open ("", a) object. Write ("write data") object .close
Example: writing data to a file
F=open ('666.txtwords') f.write ('999999') f.close ()
Open the file in (writable) form, create and add the written content from scratch if the file does not exist, and append the original data if it exists
Read and write binary files 1. Read data (rb)
Format 1:
Object = open ("binary file", rb) variable = object .read () print (variable) object .close ()
Format 2:
With open ("binary", "rb") as object: variable = object .read () print (variable)
Example 1:
F=open ('91.jpg') a=f.read () print (a) f.close ()
Example 2:
With open ('91.jpg') as f: res=f.read () print (res)
two。 Write data (wb)
Format:
With open ("binary", "wb") as object: variable = object .write () print (variable)
Example: backup picture
With open ('91.jpg res print') as f: res=f.read () with open ('66.jpg coach wb') as f: res=f.write (res) print (res)
Close the file
Format:
Close () 3. Operation of files and folders
Related operations of files
Sometimes, you need to rename or delete files, which are available in python's os module.
3.1 renaming of files
Rename () in the os module can rename the file.
Format:
Import osos.rename ("File name to be modified", "New File name")
Example:
Import osos.rename ('66.jpg recording journal 6666.jpg') 3.2 Delete files
Remove () in the os module can rename the file.
Format:
Import osos.remove ("File name")
Example:
Import osos.remove ('6666. Jpg') folder related operations 1. Create a folder
Mkdir () in the os module can rename the file.
Format:
Import osos.mkdir ('folder name')
Example:
Import osos.mkdir ('lisi') 2. Get the current directory
Getcwd () in the os module can get the current directory
Example:
Import osos.getcwd () print (os.getcwd ())
Thank you for reading! This is the end of this article on "how to manipulate files in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.