


Sample code for reading and writing files and storage in Python programming
This article mainly introduces examples of reading and writing files and storage in PythonProgramming, including examples of using cPickle storage to store objects. Friends in need can refer to the following
1. Writing and reading files
#!/usr/bin/python # -*- coding: utf-8 -*- # Filename: using_file.py # 文件是创建和读取 s = '''''我们都是木头人, 不许说话不许动!''' # 创建一个文件,并且写入字符 f = file('test_file.txt', 'w') f.write(s) f.close() # 读取文件,逐行打印 f = file('test_file.txt') while True: line = f.readline() # 如果line长度为0,说明文件已经读完了 if len(line) == 0: break # 默认的换行符也读出来了,所以用逗号取代print函数的换行符 print line, f.close()
Execution results:
我们都是木头人, 不许说话不许动!
2. Memory writing and reading
#!/usr/bin/python # -*- coding: utf-8 -*- # Filename using_pickle.py # 使用存储器 #加载存储器模块,as后面是别名 #import pickle as p #书上说cPickle比pickle快很多 import cPickle as p listpickle = [1, 2, 2, 3] picklefile = 'picklefile.data' f = file(picklefile, 'w') # 写如数据 p.dump(listpickle, f) f.close() del listpickle f = file(picklefile) # 读取数据 storedlist = p.load(f) print storedlist f.close()
Execution results:
[1, 2, 2, 3]
Let’s look at an example of using cPickle storage to store objects
#!/usr/bin/python #Filename:pickling.py import cPickle as p shoplistfile = 'shoplist.data' shoplist = ['apple', 'mango', 'carrot'] f = file(shoplistfile, 'w') p.dump(shoplist, f) f.close() del shoplist f = file(shoplistfile) storedlist = p.load(f) print storedlist
The above is the detailed content of Sample code for reading and writing files and storage in Python programming. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
