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

What are the other methods of file manipulation in python

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

Share

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

Today, I will talk to you about other methods of file operation in python. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.

General methods of file manipulation in Python, including open, write, close. This article describes some of the more common methods of file manipulation in python.

First, create a file called points:

p=open ('poems ',' r', encoding ='utf-8') for i in p:print(i) The result is as follows:

The sun is at the end of the mountain, and the Yellow River flows into the ocean current. To see a thousand miles, go up a level.

1. readline #Read a line

p=open('poems','r',encoding='utf-8')

print(p.readline())print(p.readline()) The result is as follows: hello,everyone is at the end of the day,#two newlines here, one after everyone\n, one is print's own newline2.readlines #Read multiple lines p=open ('poems ',' r', encoding='utf-8')

print(p.readlines()) #Print all the contents and the result is as follows: "'hello,everyone\n','the sun is at the end of the mountain,\n',' the Yellow River flows into the current.\ n','want to poor clairvoyant,\n',' go one level higher. ']

p=open ('poems ',' r', encoding ='utf-8') for i in p.readlines()[0:3]: print(i.strip()) #Loop the first three lines of content, removing newlines and spaces, resulting in the following: hello, world day by mountain, Yellow River into the current. 3. tell #Show current cursor position

p=open ('poems ',' r', encoding ='utf-8') print(p.tell())print(p.read(6))print(p.tell()) The result is as follows: 0hello,64.seek #You can customize the cursor position

p=open ('poems ',' r', encoding ='utf-8') print(p.tell())print(p.read(6))print (p.tell())print(p.read (6))p.seek(0)print(p.read(6)) The result is as follows: 0hello,6everyohello,5.flush #Forcibly flushes files from memory buffer to hard disk in advance while emptying buffer.

p=open ('poems1 ',' w', encoding ='utf-8') p.write ('hello.world') p.flush()p.close()#Write the file to the hard disk before close. Normally, the file will be automatically flushed to the hard disk after closing, but sometimes you need to flush it to the hard disk before closing. In this case, flush() method can be used. 6. truncate #Keep p=open ('points ',' a', encoding ='utf-8') p.truncate(5)p.write ('tom ') The result is as follows: hellotom#Keep the first five characters of the file points, empty the following contents, and add tom

After reading the above, do you have any further understanding of what other methods of file manipulation are in python? If you still want to know more knowledge or related content, please pay attention to 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.

Share To

Development

Wechat

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

12
Report