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

File manipulation of python and examples of Pick enclosure

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "python file operation and Pick storage module examples", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "python file operations and Pick storage module examples" bar!

File operation

Enter:

#! / usr/bin/python

# Filename: using_file.py

Poem =''\ Programming is funWhen the work is doneif you wanna make your work also fun: use Python

F = open ('poem.txt', 'w')

# open for 'w'riting

F.write (poem)

# write text to file

F.close ()

# close the file

F = open ('poem.txt')

# if no mode is specified, 'r'ead mode is assumedby default

While True:

Line = f.readline ()

If len (line) = = 0: # Zero length indicates EOF

Break

Print (line, end='')

F.close ()

# close the file

Output:

$python using_file.py

Programming is fun

When the work is done

If you wanna make your work also fun:

Use Python!

Explanation:

In this case, the read and write operation of the file is designed.

First, define a string, open a file, save the data, and save it.

Then open the file you just saved and output the contents by line.

Pickle module

Enter:

#! / usr/bin/python

# Filename: pickling.py

Import pickle

# the name of the file where we will store the objectshoplistfile = 'shoplist.data'

# the list of things to buy

Shoplist = ['apple',' mango', 'carrot']

# Write to the file

F = open (shoplistfile, 'wb')

Pickle.dump (shoplist, f)

# dump the object to a file

F.close ()

Del shoplist

# destroy the shoplist variable

# Read back from the storage

Output:

$python pickling.py

['apple',' mango', 'carrot']

Explanation:

Python provides a standard module called pickle, which you can use to store any Python

The object in the file, and then retrieve it. This is called a persistent storage object.

Thank you for your reading, the above is the content of "python file operation and Pick enclosure instance". After the study of this article, I believe you have a deeper understanding of python file operation and Pick enclosure instance, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report