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

python installation method and a brief introduction to IO programming

不言
Release: 2018-10-12 15:24:56
forward
2469 people have browsed it

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()
Copy after login

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())
Copy after login

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)
Copy after login
## 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)
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template