Python file interaction, module and pickle serialization, regular expression re module

Source: Internet
Author: User
Tags switches

Let's say something else.

sudo to run commands with super privileges

Su switches to the root user

sudo su switches to root user with super privilege

That's the difference between them.

Read the file:

f = File (' Abc.txt ') is the same as F = open (' Abc.txt ') method.

f = File (' Abc.txt ', W) open a write file, open a read file by default without parameters.

Modes are:

R # # W # # a # rw # # RB # # WB # # ab##

F.read () returns a string

F.readlines () return list

F.readline () take out a row

F.close () Close file handle

F.flush () flush cache Instant Output

F.write () writes a string to a file

Shell color settings: \033[32;1m ... \033[0m

Import Fileinput

Line=fileinput.input (' Abc.txt ', inplace=1) #inplace的作用是文件指针返回到文件开头

Line=line.replace (' A ', ' a ') #将文件中的所有A替换成a

The pop () of the list takes the position as the parameter, and remove () takes the specific value as the parameter.

IS and = =.

Shell command: SSH [email protected] cmd

If the file name is a B, it can be entered by the command CD A\ B.

Definition of the default parameters for Python: Backward forward.

Join usage: ' + '. Join (a) # # A refers to a string or list

Pickle Module Usage:

# # Deposit data to pickle file # #

Import Pickle

a={"Name": "XXX", "Age": "Occupation": "IT",}

F=file (' abc.pkl ', ' WB ')

Pickle.dump (A,F)

F.close ()


# # Remove data from the Pickle file # #

F=file (' abc.pkl ', ' RB ')

A=pickle.load (f)

F.close ()

Re regular expression matching module:

Import re

P=re.compile (R ' Hello ')

Re.match # # Match start

P.findall # #找到所有匹配到该模式的内容

Re.sub (' [ABC] ', ' o ', Mark) # # Replaces all A, B, and C in Mark with O.

SSH related modules: Paramiko and Fabric

SYS module:

Import Sys

Print SYS.AGRV # # Sys.argv[0] is the program name.

Sys.path # #寻找路径

Python modules are generally placed under the/usr/lib/python2.7/dist-packages/directory.

Finish.






This article is from the "Blue Fin Dolphin" blog, please be sure to keep this source http://likunheng.blog.51cto.com/9527645/1571883

Python file interaction, module and pickle serialization, regular expression re module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.