Python common module sys, os, time, random function and usage examples Analysis

This article introduces the common Python module sys, os, time, random function and usage, analyzing the Python module sys, os, time, random function, principle, form related modules function with examples, tips and operation precautions need friends can refer to the
examples herein describes Python common module sys, os, time, random function and usage. Share to you for your reference, as follows:

sys:
Profile: python compiler contains functions involve interaction with the system.

Common functions:

import sys
print(sys.argv)#本文件名,已经运行该程序时的参数
#[如在命令窗口中python3 mysys.py 参数1 参数2]
#那么参数1为sys.argv[1],以此类推
print(sys.version)#python版本号
print(sys.path)#返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
# sys.exit(0)#中途退出程序,当参数非0时,会引发一个SystemExit异常
sys.stdout.write()#在屏幕中打印
sys.stdout.flush()#刷新标准缓冲区

os:
Description: This module provides a convenient method for using the operating system function.

Common functions:

import os
print("-------getcwd()获取当前目录-------")
print(os.getcwd())
print("-------chdir()改变当前所在目录-------")
# print(os.chdir("c://users"))#c:\\users  r'c:\users'
# print(os.getcwd())
print("------ . .. --------")
print(os.curdir)   #打印出 .
print(os.pardir)   #打印出 ..
print("-------makedirs递归创建目录-------")
#os.makedirs(r"c:\a\b\c")   #要创建c,如果a不存在则创建a,如果b不存在则创建b
print("-----remodir递归删除目录---------")
#os.removedirs(r"c:\a\b\c")  #清除空文件夹,从c到a,如果a,b也是空的话也会删除。
print("------mkdir创建目录--------")
# os.mkdir('c://a')
print("--------listdir列出指定目录下的所有文件和子目录------")
print(os.listdir())
print("--------remove删除文件------")
# print(os.remove('c://newfile'))
print("-------rename文件重命名-------")
# os.rename('oldname','newname')
print("-------stat 获取文件或目录信息-------")
print(os.stat('.'))
print("------sep 输出操作系统特点的路径分割符--------")
print(os.sep)
print("-----linesep 输出当前平台的行终止符---------")
list1=[]
list1.append(os.linesep)
print(list1)
print("------pathsep 输出用于分割文件的字符串--------")
print(os.pathsep)
print("----------name输出操作平台----------")
# print(os.name)#nt
print("-------system执行shell命令-------------")
print(os.system("dir"))
print("----------path关于文件和目录的操作----------")
# print(os.path.abspath(__file__))###返回绝对路径
print(os.path.split(os.path.abspath(__file__)))##将路径切割成目录名和文件名
print(os.path.dirname(os.path.abspath(__file__)))#只取路径名
print(os.path.dirname(__file__))###__file__是包括完整路径名的,也是绝对路径
print(os.path.basename(__file__))#只取文件名
print(os.path.exists("c://a"))#判断路径是否存在,不区分目录或文件
print(os.path.isabs(__file__))#判断是否是绝对路径
print(os.path.isfile("c://amd"))#判断是否是文件
print(os.path.join(r'c:',r'\a.txt'))#组合绝对路径
print("----------environ获取当前系统所有环境变量----------")
print(os.environ)
print("---------popen() 方法用于从一个命令打开一个管道-----------")
print(os.popen('dir').read())##主要用于处理执行命令的返回结果
print("获取进程号".center(50,'-'))
print(os.getpid())#获取当前进程号
print(os.getppid())#获取父进程号

Note:
os.system The main difference is that the former os.popen with the return value is the exit status of the script, which returns a file descriptor is stored in the output value of the contents of the script execution process.
Here Insert Picture Description
Attachment:

python subprocess module is a module version 2.4 from the start of introduction. It is mainly used to replace some of the old method of modules, such as os.system, os.spawn *, os.popen *, commands. * And the like. subprocess to execute external commands subprocess, through input / output / error pipes, sub-process execution obtain return information.
time:
Description: Contains a function of time

Common functions:

import time
print("--------时间戳-------------")
print("时间戳time:",time.time())#时间戳time: 1516435471.756463
print("----------结构化时间(tm_year=2018, tm_mon=1.....-----------")
print("struct_time:",time.gmtime(time.time()))#tm_year=2018, tm_mon=1.........
print("timestamp->struct_time:",time.gmtime())#UTC时间
print("local_time:",time.localtime())#本地时区时间
print("struct_time->timstamp:",time.mktime(time.gmtime()))#结构化时间-->时间戳
print("----------ctime,asctime--------")
print("string_time:",time.ctime())###字符串时间 Mon Feb 5 01:02:06 2018
print("asctime:",time.asctime())###字符串时间 Mon Feb 5 01:02:06 2018
print("----------format_time格式化时间、struct_time-----------")
#结构化时间转格式化时间:%Y代表year,%m代表month,%d代表day, %H代表hour,%M代表minute,%S代表second
#只会取代%Y等字符,并不替换无对应意义的字符
print("struct_time -> format_time:\n", time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
y=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
#格式化时间转结构化时间
print("format_time -> struct_time:\n",time.strptime(y,"%Y-%m-%d %H:%M:%S"))
print("------------year--------------")
print("year:",time.localtime().tm_year)

random:
Introduction: stores function on the "random"

Common functions:

import random
print("---------0到1,随机浮点值-----------")
print(random.random())
print("------------从范围中,随机取值,1<=x<=2--------")
print(random.randint(1,2))
print("------------从指定范围中,随机取值--------")
print(random.randrange(1,3))
print("------------从序列中,随机值--------")
print(random.choice("hello"))#从序列中随机取值
print(random.choice([0,11,3,99]))
print("------------从序列中,随机取指定个数值--------")
print(random.sample('heigo',2))#
print("------------随机取浮点值,start,end--------")
print(random.uniform(1,2))#start,end
print("-------洗牌,打乱排序-----")
l=[0,3,4,5,67,9]
random.shuffle(l)
print(l)

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining

And other programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Every day, veteran

Python method to explain the timing of technology, to share some of the learning and the need to pay attention to small details, click on Join us python learner gathering

Published 27 original articles · won praise 22 · views 20000 +

Guess you like

Origin blog.csdn.net/haoxun05/article/details/104382881