Python攻关之模块(1)
一、os模块
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2') 可生成多层递归目录
os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.environ 获取系统环境变量
os.path.abspath(path) 返回path规范化的绝对路径
os.path.split(path) 将path分割成目录和文件名二元组返回
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
二、sys模块
sys 都是和解释器进行交互
sys.argv 命令行参数List,第一个元素是程序本身路径sys.exit(n) 退出程序,正常退出时exit(0)sys.version 获取Python解释程序的版本信息sys.maxint 最大的Int值sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量值sys.platform 返回操作系统平台名称sys.stdout.write('please:')val = sys.stdin.readline()[:-1]
sys实现进度条
#sys实现进度条
import sys,time
for i in range(30):sys.stdout.write('*')sys.stdout.flush()time.sleep(0.1)
三、hashlib模块
用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1,SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import hashlib######## md5 ########hash = hashlib.md5()
hash.update('admin')
print(hash.hexdigest())######## sha1 ########hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())######## sha256 ########hash = hashlib.sha256()
hash.update('admin')
print(hash.hexdigest())######## sha384 ########hash = hashlib.sha384()
hash.update('admin')
print(hash.hexdigest())
四、logging模块
https://www.cnblogs.com/yuanchenqi/articles/5732581.html
五、configparser模块
#生成example.ini 的配置文件import configparserconfig = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45','Compression': 'yes','CompressionLevel': '9'}config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:config.write(configfile)
配置文件的读
import configparserconfig = configparser.ConfigParser()#---------------------------------------------查
print(config.sections()) #[]config.read('example.ini')print(config.sections()) #['bitbucket.org', 'topsecret.server.com']print('bytebong.com' in config)# Falseprint(config['bitbucket.org']['User']) # hgprint(config['DEFAULT']['Compression']) #yesprint(config['topsecret.server.com']['ForwardX11']) #nofor key in config['bitbucket.org']:print(key)# user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]print(config.get('bitbucket.org','compression'))#yes
#configparser的增删改查
config.remove_section('topsecret.server.com') #删出整个键值对
print(config.has_section('topsecret.server.com')) #判断是否在config.remove_option('bitbucket.org','user') #删除某个键值对底下的键值对#config.set('bitbucket.org','user','alex') #修改config.write(open('example.ini',"w"))
Python攻关之模块(1)相关推荐
- Python攻关之模块(2)
一.re模块 string 提供的方法是完全匹配 引入正则:模糊匹配 import re eg: ret = re.findall('w\w{2}l','hello world') print(ret ...
- python:Json模块dumps、loads、dump、load介绍
20210831 https://www.cnblogs.com/bigtreei/p/10466518.html json dump dumps 区别 python:Json模块dumps.load ...
- 能带曲线图绘制python_如何使用python的matplotlib模块画折线图
python是个很有趣的语言,可以在cmd命令窗口运行,还有很多的功能强大的模块.这篇经验告诉你,如何利用python的matplotlib模块画图. 工具/原料 windows系统电脑一台 pyth ...
- python之路——模块和包
一.模块 1.什么是模块? 常见的场景:一个模块就是一个包含了Python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1.使用Python编 ...
- Python multiprocess 多进程模块
转发:http://www.langzi.fun/Python multiprocess 多进程模块.html 需要注意的是,如果使用多线程,用法一定要加上if __name__=='__main__ ...
- Python 安装 xlsx模块
为什么80%的码农都做不了架构师?>>> Python 安装 xlsx模块 很多时候自动化测试时测试用例是写在excel中的如何读取转换成字典是一个比较关键的问题,使用pip命 ...
- python时间处理模块 datetime time模块 deltetime模块
1 首先介绍time模块,因为简单 python 自带模块 本人使用time模块,只使用两个函数 time函数和sleep函数 import time a. time.time() 函数 ...
- python使用joblib模块保存和加载机器学模型
python使用joblib模块保存和加载机器学模型 # 导入需要的包和库: # Import Required packages #-------------------------# Import ...
- python时间处理模块datetime+dateutil、numpy时间处理模块datetime64以及pandas时间处理模块Timestamp的演化路径及常用处理接口
python时间处理模块datetime+dateutil.numpy时间处理模块datetime64以及pandas时间处理模块Timestamp及常用处理接口 python时间处理模块dateti ...
最新文章
- python统计excel出现次数_Python读取Excel一列并计算所有对象出现次数的方法
- 【SSH框架】之Hibernate系列一
- Jacobian矩阵和Hessian矩阵的理解
- Java学习路线详解
- 程序员你真的理解final关键字吗?
- QCombox隐藏某一项
- Wireshark数据抓包分析之互联网协议(IP协议)
- fastdfs连接mysql_使用fastdfs-zyc监控FastDFS文件系统
- 9.4 网易互娱客户端笔试
- 干货 | 关于SwiftUI,看这一篇就够了
- automagica 调用windows画图以及登录qq
- vue 中 mixins 的使用
- 使用 OpenCV 识别图片中的猫咪
- go postgresql 增删改查
- 投影仪幕布增益_你还对投影仪幕布不了解吧?这些干货让你明白
- 干法读书心得:第一章 “极度”认真地工作能扭转人生
- pyinstaller流程及相关问题
- 指向指针的指针的内存分配 author:Rajesh R Nair.
- 【Java笔记】File类与IO流(另添加NIO2使用)
- 7种经典的统计学谬论