Home  >  Article  >  Backend Development  >  python installation method and a brief introduction to IO programming

python installation method and a brief introduction to IO programming

不言
不言forward
2018-10-12 15:24:562428browse

This article brings you a brief introduction to the installation method of python and IO programming. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

一.python installation

1.python IDLE

Download official website: www.python.org

Note: After selecting When installing components, check all components, pay special attention to check pip and Add python.exe to Path

2.pycharm

Download official website: https://www.jetbrains.com/pycharm/

A Python IDE created by JetBrains, which also supports Google App Engine, IronPython

3.Anaconda

Download official website: https://www.anaconda.com/ download/

An open source Python distribution, including a large number of installed scientific packages such as: numpy, pandas, etc.

2. IO programming

1. File reading and writing

Open the file:

The mode parameter in the open function:

## The buffering parameters in the open function:

File reading and writing: Frequently used methods include read(), readlines() ,write(),close()


read(size) reads up to size bytes at a time and returns a string; readlines() reads the entire content at a time and returns a list by line

The file must be closed after use! Use try...finally to perform file operations to prevent IO exceptions

try:
    f = open("D:/Python/test.txt","r+")  # 'r+' == r+w(可读可写,文件若不存在就报错(IOError))
    print(f.read())
    f.write("def")
    f.seek(0,0)                          # 把文件指针从末尾移到开头
    print(f.read())
finally:
    if f:
        f.close()

In python, you can use the with statement to replace the try...finally code block and close() method

with open("D:/Python/test.txt","r+") as f:    
print(f.read())
    f.write("def")
    f.seek(0,0)                        
    print(f.read())

2. Directory operation

Commonly used modules: os module and shutil module

3. Serialization

The process of turning variables in memory into storable or transmissible is serialization

The pickle module is used in python to implement serialization, mainly using the dump method (writing the serialized object directly into a file) or the dumps method (serializing any object into a str and then writing it to a file for storage)

import pickle
d = dict(url="index.html",title="首页",content="首页")print(pickle.dumps(d))
with open("D:/Python/test.txt","wb") as f:
    pickle.dump(d,f)
## The #pickle module mainly uses the load method (to deserialize files directly into objects) or the loads method (to deserialize str into objects) to implement deserialization

with open("D:/Python/test.txt","rb") as f:
    d = pickle.load(f)print(d)
d = pickle.loads(b'\x80\x03}q\x00(X\x03\x00\x00\x00urlq\x01X\n\x00\x00\x00index.htmlq\x02X\x05\x00\x00\x00titleq\x03X\x06\x00\x00\x00\xe9\xa6\x96\xe9\xa1\xb5q\x04X\x07\x00\x00\x00contentq\x05h\x04u.')print(d)

The above is the entire content of this article, about For more exciting content on python, you can pay attention to the

Python video tutorial

and python article tutorial columns on the php Chinese website! ! !

The above is the detailed content of python installation method and a brief introduction to IO programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete