Python之logging,os,sys模块简析

来源:互联网 发布:grub引导修复 ubuntu 编辑:程序博客网 时间:2024/06/09 23:30

Python之logging,os,sys模块简析

1.logging模块

#!/usr/bin/env python

# -*- coding:utf8 -*-

# @Time     : 2017/11/10 10:00

# @Author   : hantong

# @File     : log.py

import logging

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='.log', filemode='w')

#此参数很长,建议拷贝下来使用时直接粘贴,包含日志格式的设定,日志信息写入文件等信息

logger = logging.getLogger(__name__)

#打印时时的log信息,写入日志时清空之前的内容

logging.debug('this is debug message')

logging.info('this is info message')

logging.warning('this is warning message')

logging.error('this is error message')

logging.critical('this is critical message')

#以上是日志的几个级别

代码输出为空,但是建立了一个logging.log的文件,并且日志写入此文件

2.os模块

#!/usr/bin/env python

# -*- coding:utf8 -*-

# @Time     : 2017/11/10 14:10

# @Author   : hantong

# @File     : os_test.py

import os

# print(os.name)

#windows系统显示的是‘nt’,linux系统显示的是‘posix’

# cont = os.popen('ipconfig').read()

# print(cont)

#查看windows系统网卡信息,由于系统语言格式问题,显示出来乱码


print(os.listdir('.'))

#查看当前目录文件

print(os.getcwd())

#查看当前路径

print(os.listdir(os.getcwd()))


os.chdir(r'E:\Xshell 5')

#切换目录到E:\Xshell5

print(os.getcwd())

#查看是否切换成功


# os.mkdir('txt1111')

#新建一个文件名为txt1111的文件

# os.remove('txt111')

#删除一个文件

print(os.linesep)

#打印操作系统的分隔符

if not os.path.exists('txt11'):

#os.path.exists('txt11')检查是否存在txt11这个文件

    os.makedirs('txt11')

#新建txt11文件,连同目录一起建立

#os.makedirs()

else:

    print('txt11 directory is ok!')


path = os.path.join(os.getcwd(),'abc')

#拼接目录

print(path)

print(os.path.dirname(r'E:\Xshell 5'))

#查看根目录

执行结果:

['.idea', '.log', '1.txt', '2.txt', 'buer.py', 'class.py', 'class2.py', 'class3.py', 'class4.py', 'class5.py', 'command_test.py', 'convert.py', 'daetime2.py', 'datetime.py', 'def.py', 'dict.py', 'except.py', 'file.py', 'float.py', 'for.py', 'func.py', 'func2.py', 'genarator.py', 'help.py', 'hhh.py', 'if.py', 'lianxi.py', 'list', 'list.py', 'list.txt', 'log.py', 'myapp.log', 'os_test.py', 'os_test.pyc', 'revise.py', 'str.py', 'sys_test.py', 'test.py', 'test.pyc', 'test2.py', 'test3.py', 'test4.py', 'threed_week.py', 'tuple.py', 'txt', 'txt3', 'while.py']

E:\Ǩ������\python\pycharm2017pjb\PycharmProjects

['.idea', '.log', '1.txt', '2.txt', 'buer.py', 'class.py', 'class2.py', 'class3.py', 'class4.py', 'class5.py', 'command_test.py', 'convert.py', 'daetime2.py', 'datetime.py', 'def.py', 'dict.py', 'except.py', 'file.py', 'float.py', 'for.py', 'func.py', 'func2.py', 'genarator.py', 'help.py', 'hhh.py', 'if.py', 'lianxi.py', 'list', 'list.py', 'list.txt', 'log.py', 'myapp.log', 'os_test.py', 'os_test.pyc', 'revise.py', 'str.py', 'sys_test.py', 'test.py', 'test.pyc', 'test2.py', 'test3.py', 'test4.py', 'threed_week.py', 'tuple.py', 'txt', 'txt3', 'while.py']

E:\Xshell 5



txt11 directory is ok!

E:\Xshell 5\abc

E:\

#上面乱码部分是中文导致的

3.commmands模块

#此命令必须在linux系统上才能执行

#!/usr/bin/env python

#-*-coding:utf-8 -*-

#只能在linux下使用

import commands

cmd = 'ls /home'


result = commands.getoutput(cmd)


print(type(result))

print(result)


#commands.getoutput的返回值只有返回结果,没法进行判断执行结果是否正常


cmd1 = 'ps -ds|fial'

status,result = commands.getstatusoutput(cmd1)

print(type(result))

print(result)

print(type(status))

print(status)

#commands.getstatusoutput的返回值是一个tuple类型

执行结果不再贴出来,因为太长

4.sys模块


阅读全文
0 0
原创粉丝点击