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模块
- Python之logging,os,sys模块简析
- python 的logging ,os模块, command模块 , sys模块
- python学习—Day20—模块:logging、os、command、sys
- Python初学之sys、os、platform模块
- python模块-os,sys
- python之路——sys模块与os模块
- Python os sys模块使用
- Python 中的os、sys模块
- python 之logging模块
- Python模块之logging
- python模块之 - logging
- python之logging模块
- python之logging模块
- PYTHON模块之sys
- python 之 sys模块
- 【Python】标准库模板os, sys, platform, logging
- python常见模块命令(os/sys/platform)
- python常见模块命令(os/sys/platform)
- 题目:JavaScript编程题 页面上输入一个年份(需验证),判断是否是闰年(能被4整除却不能被100整除的年份;能被400整除的是闰年),需在页面上显示提示信息
- ubunut14.04配置caffe时出现 error while loading shared libraries: libopencv_core.so.3.2: cannot open share
- Spring Boot(三)——Redis
- 网狐荣耀版安卓端的签名与打包
- UITableView的 beginUpdates 和 endUpdates
- Python之logging,os,sys模块简析
- socket阻塞与非阻塞,同步与异步、I/O模型
- 纯CSS气泡框实现方法探究
- Leetcode481. 魔力字符串
- Joint Sequence Learning and Cross-Modality Convolution for 3D Biomedical Segmentation笔记
- 关于Lua调用C的简单demo
- 阿里云centos环境之linux上Jenkins安装<六>
- Linux下VirtualBox虚拟机的命令行启动/关闭方法和开机自动启动
- Not found in archive错误