IS & python read and write files and common module == & distinction

and is the difference between ==

  • Not only is the data memory address is the same as
  • == Analyzing only to the same type of data and data

Reading and Writing Files

  • Classic face questions: There is a file of 5G, another file is written in python

    • read (): Specifies the size of the specified file to read (the default time to read all)
    • readline (): read line by line, for reading large files
    • readlines (): one-time read all the files, the file is read into a list of rows
  • We use a while loop to read the contents of the file, the size of each read up to 8kb

  • You need a huge string of splicing process to avoid before, to reduce memory footprint very much.

#!/usr/bin/python
# -*- coding: utf-8 -*-
def read_big_file_v(fname):
    block_size = 1024 * 8
    with open(fname,encoding="utf8") as fp:
        while True:
            chunk = fp.read(block_size)
            # 当文件没有更多内容时,read 调用将会返回空字符串 ''
            if not chunk:
                break
            print(chunk)
path = r'C:\aaa\luting\edc-backend\tttt.py'
read_big_file_v(path)

Common Module

  • re module
findall: findall(pattern,string),查找所有满足条件的字符

search: search(pattern,string[,flags]),在字符串中查找,返回第一个匹配的字符串 分装返回对象为,span = (0,5) (匹配的位置)左闭右开

match : match(pattern,string[,flags]) 在字符串开头查找,与search返回值相同

split:split(pattern,string[,maxsplit=0])根据模式切割字符串

compilecompile(pattern,[,flags]) 根据包含正则表达式的字符串创建模式对象

sub : sub(pat,repl,string[,count=0]) 将字符串中模式pat匹配的子串都替换为repl

  • subprocess module

subprocess principle and common wrapper function

  • Run python, we are in a process to create and run. Like Linux, as process, a process can fork a child process, and let the child process exec another program
  • In Python, we adopted the standard library subprocess package to fork a child process, and to run an external program.
  • subprocess defined in the package are several function creates the child process, these functions are to create a child process in a different way, so we can choose one to use as needed
  • In addition subprocess also provides some management standard flow (standard stream) and tools for pipe (pipe), which use text communication between processes.

subprocess commonly used functions

#1、返回执行状态:0 执行成功
retcode = subprocess.call(['ping', 'www.baidu.com', '-c5'])

#2、返回执行状态:0 执行成功,否则抛异常
subprocess.check_call(["ls", "-l"])

#3、执行结果为元组:第1个元素是执行状态,第2个是命令结果
>>> ret = subprocess.getstatusoutput('pwd')
>>> ret
(0, '/test01')

#4、返回结果为 字符串 类型
>>> ret = subprocess.getoutput('ls -a')
>>> ret
'.\n..\ntest.py'


#5、返回结果为'bytes'类型
>>> res=subprocess.check_output(['ls','-l'])
>>> res.decode('utf8')
'总用量 4\n-rwxrwxrwx. 1 root root 334 11月 21 09:02 test.py\n'

Convert dos format file into a unix format

subprocess.check_output(['chmod', '+x', filepath])
subprocess.check_output(['dos2unix', filepath])
Published 84 original articles · won praise 1 · views 2064

Guess you like

Origin blog.csdn.net/lxp_mocheng/article/details/104796396