Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement File manipulation by python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "python how to achieve file operation", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to achieve file operation" this article.

General content of file operation:

# File operation # Open file open open an existing file or create a new file open ('. / Test.txt','w')

# File operations # Open file open open an existing file or create a new file fobj=open ('. / Test.txt','w') # pass it to an object to manipulate it through an object # start operation read / write operation fobj.write ('there is a flock of seabirds on the vast sea') fobj.close () # Save and close

# Operation of file # Open file open, open an existing file or create a new file fobj=open ('. / Test.txt' 'w') # pass it to an object to operate on it through the object # start the read / write operation # fobj.write (' there is a flock of seabirds on the vast sea') fobj.write ('break through the storm') # the file will be overwritten fobj.close () # Save and close

# File operation # Open file open open an existing file or create a new file # default encoding is the best habit of gbk Chinese encoding is that we specify an encoding when we open a file # give him an encoding type fobj=open ('. / Test.txt','w' Encoding='utf-8') # pass it a = to an object to operate on it through the object # start the read / write operation fobj.write ('there is a flock of seabirds on the vast sea') fobj.write ('break through the storm') # the file will be overwritten fobj.close () # Save and close # write data fobj=open in binary form ('Test.txt'') 'wb') # str-- > bytesfobj.write (' no more canals between the sea '.encode (' utf-8')) fobj.close ()

Fobj=open ('. / Test.txt','w') fobj.write ('there is a flock of seabirds on the vast sea') fobj.write ('break through the wind and waves') # file existence will be overwritten fobj=open ('Test.txt','a') # add fobj.write (' between dark clouds and sea') fobj.close ()

# read data operation f=open ('Test.txt','r') # print (f.read ()) # read out print (f.read (10)) from beginning to end # specify the number of reads print (f.read ()) # read the first behavior plus parameters, and output the rest of the lines

F=open ('Test.txt','rb') data=f.read () print (data) print (data.decode (' gbk')) f.close () # File object needs to be closed

# with context management object # advantage automatically release open associated object with open ('Test.txt','r') as f: print (f.read ())

The above is all the contents of the article "how to manipulate files in python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report