File and directory operations

Create, delete, modify, concatenate, obtain the current directory, traverse the files in the directory, obtain the file size, modify the date, determine whether the file exists, etc.

Ii. Date and time (built-in modules: Time, Datatime, Calendar)

Time.time () # return the number of seconds since 0 o ‘clock on January 1, 1970 to the current time

Example 1: Gets the execution time of a function, in seconds

Import time before = time.time() func1 after = time.time() print(f" call func1, spend time {after-before}")Copy the code

2. Datetime.now () # return the current time

from datetime import datetime
print(datetime.now())
Copy the code

Output result: 2020-06-27 15:48:38.400701

3. Display the string in the specified format

datetime.now().strftime('%Y-%m-%d -- %H:%M:%S')

time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
Copy the code

3. Call other programs in Python programs

Python calls external programs using the system function of the standard OS library or the subproProcess library.

1. Wget (WGET is a free tool that automatically downloads files from the network through HTTP, HTTPS, FTP three most common TCP/IP protocol download)

1) Brew install wget on MAC

2) wget –help/wget -h

3) use wget to download files, download file to the current directory, MAC terminal command: wget mirrors.sohu.com/nginx/nginx…

2. OS. The system function

1) OS. system calls external programs, and the execution of the called program must be completed before it can continue

2) The os.system function cannot get the contents of the called program output to the terminal window

Wget import OS CMD = 'http://mirrors.sohu.com/nginx/nginx-1.13.9.zip' OS. The system (CMD) - version = input (' please enter the installer version: ') cmd = fr'd:\tools\wget <a href="http://mirrors.sohu.com/nginx/nginx-{version}.zip'os.system(cmd" rel="external nofollow">http://mirrors.sohu.com/nginx/nginx-{version}.zip' os.system(cmd</a>)Copy the code

3. The subprocess module

Example 1: Pipe the information that should have been output at the terminal and analyze it

Proc = Popen('du -sh *', stdin = None, stdout = PIPE, stderr = PIPE, shell=True) outinfo, Communicate () # Communicate method returns output to standard output and standard error string content outinfo = outinfo.decode(' GBK ') errinfo = errinfo.decode('gbk') outputList = outinfo.splitlines() print(outputList[0].split(' ')[0].strip())Copy the code

Example 2: Start wget to download files

from subprocess import Popen

proc = Popen(

    args='wget http://xxxxserver/xxxx.zip',

    shell=True

  )
Copy the code

With subprocess, you do not need to wait for the external program to finish executing and can continue executing other programs

Four, multi-threading

If you are writing automated test cases, you can use pyTest test framework, with its own multi-threaded implementation method.

The above is all the content of this article, I hope to help you learn, but also hope that you support.