2018年04月25日 logging模块os.name模块sys模块random模块string模块
Posted ksqinglong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018年04月25日 logging模块os.name模块sys模块random模块string模块相关的知识,希望对你有一定的参考价值。
logging模块
官方文档:https://docs.python.org/3.6/library/logging.html
‘‘‘ 日志记录模块 日志的几个级别 debug info warning error critical ‘‘‘ import logging # logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG, format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,datefmt=‘ %Y/%m/%d %H:%M:%S‘, filename=‘myapp.log‘, filemode=‘w‘) logger = logging.getLogger(__name__) def hello(): print("hello world") def main(): logger.info("开始执行main函数") print("##"*10) hello() logger.info("调用hello() 函数") try: a = 2/0 f = open("demon1.py", "r") except Exception as e: logger.error("除数不能为0") finally: logger.warning("文件没有正常关闭") main()
os模块
官方文档:https://docs.python.org/3.6/library/os.html
os.name
‘‘‘ os模块 os.name 如果结果为nt, 则为windows系统, 如果结果为posix, 则为unix系统 os.system(cmd) 纯粹的执行系统命令,但是没有返回结果 result = os.popen(cmd) result.read() 这样你就可以对reuslt进行控制了 os. ‘‘‘ import os print(os.name) if os.name == "nt": cmd = "ipconfig" elif os.name == "posix": cmd = "ifconfig" # os.system(cmd) print(os.listdir("C:")) #列出当前目录, ls # os.chdir("..") #改变目录, cd print(os.listdir()) print(os.getcwd()) #pwd # os.mkdir("test") # os.remove("myapp.log") # os.rmdir("test") # os.rename("demon1.py", "demon111.py") print(os.linesep) # windows换行符\n\r linux换行符\n mac \r if not os.path.exists("test"): os.mkdir("test") print(os.path.abspath("./")) print(os.path.split("E:\LivePython1\第十一课"))
sys模块
官方文档:https://docs.python.org/3.6/library/sys.html
‘‘‘ sys ‘‘‘ import sys print(sys.argv[1]) print(sys.argv[2]) print(sys.stdout) sys.stdout.write("allalallala") f = open("1.log", "w") sys.stdout = f print("hello world")
random模块
官方文档:https://docs.python.org/3.6/library/random.html
‘‘‘ random 随机模块 random.randint(a, b) ‘‘‘ import random print(random.randrange(1, 100, 2)) print(random.sample([1, 2, 3, 4, 5, 6, 7], 2)) class NumberCount(object): def __init__(self): self.number1 = 0 self.number2 = 0 self.number3 = 0 self.number4 = 0 self.number5 = 0 self.number6 = 0 def count(self): for i in range(1, 6001): number = random.randint(1, 6) if number == 1: self.number1 += 1 if number == 2: self.number2 += 1 if number == 3: self.number3 += 1 if number == 4: self.number4 += 1 if number == 5: self.number5 += 1 if number == 6: self.number6 += 1 def getResult(self): print("1出现的次数: {0}".format(self.number1)) print("2出现的次数: {0}".format(self.number2)) print("3出现的次数: {0}".format(self.number3)) print("4出现的次数: {0}".format(self.number4)) print("5出现的次数: {0}".format(self.number5)) print("6出现的次数: {0}".format(self.number6)) if __name__ == "__main__": numberCount = NumberCount() numberCount.count() numberCount.getResult()
string模块
官方文档:https://docs.python.org/3.6/library/random.html
import random import string # print(string.ascii_letters) # print(string.digits) # print(string.ascii_lowercase) # print(string.ascii_uppercase) # print(string.printable) # print(string.punctuation) # print(string.hexdigits) # print("".join(random.sample(string.ascii_letters + string.digits, 4)))
以上是关于2018年04月25日 logging模块os.name模块sys模块random模块string模块的主要内容,如果未能解决你的问题,请参考以下文章