Python基础中os和数据结构是怎么样的
发表于:2024-10-09 作者:千家信息网编辑
千家信息网最后更新 2024年10月09日,Python基础中os和数据结构是怎么样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。今天总结了下Python的基础,
千家信息网最后更新 2024年10月09日Python基础中os和数据结构是怎么样的
Python基础中os和数据结构是怎么样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
今天总结了下Python的基础,发现还是有很多基础需要巩固,直接把学习的内容放上来。
>>> import os得到当前的所在的路径>>> os.getcwd() '/root/test' 列出当前路径所在的文件夹下的文件>>> os.listdir(os.getcwd())['a.py', 'redis_test.sql', 'cmdb_server.txt', 'a.sql', 'test.py', 'redis_test.txt', 'paramiko.pyc', 'cmdb_server.txt.bak', 'paramiko.py', 'requirements_add.txt', 'test.txt', 'opsmanage.tar.gz', 'test.sql']返回当前的绝对路径>>> os.path.abspath('.') '/root/test' 得到当前路径上一次的绝对路径>>> os.path.abspath('..') '/root' 把路径分解为路径和文件名>>> os.path.split('/root/test/test.py')('/root/test', 'test.py')>>> os.path.split('.')('', '.')将路径进行合并>>> os.path.join('/root/test','test.py') '/root/test/test.py' 返回指定path的文件夹部分>>> os.path.dirname('/root') '/' 返回当前path的文件夹>>> os.path.dirname(os.getcwd()) '/root' 得到当前的路径,和上面的可以互为印证>>> os.getcwd() '/root/test' 返回path中的文件名>>> os.path.basename('/root/test/test.py') 'test.py' 返回path中的子文件夹>>> os.path.basename('/root/test') 'test' >>> os.path.basename('/root/test/') '' 得到文件或文件夹的最后修改时间>>> os.path.getmtime('/root/test/test.py') 1521193690.4832795 得到文件或文件夹的大小,注意文件夹的部分得到的可能不是真实的大小,不是du -sh 类似的结果>>> os.path.getsize('/root/test/test.py') 29 查看文件或者文件夹是否存在>>> os.path.exists('/root/test/test.py') True >>> os.path.exists('/root/test/test.py22') False
一些路径在不同操作平台的表示>>> os.sep '/' >>> os.extsep '.' >>> os.linesep '\n' >>> os.pathsep ':'
得到目录下的文件>>> os.listdir(os.getcwd())['dict.py', 'sqlplan.py', 'deploy.pyc', 'task_manage.py', 'cron.py', 'mysql_manage.py', 'system_manage.pyc', 'cmdb.pyc', 'deploy.py', 'ansible.pyc', 'index.py', 'tuning.ini', 'cron.pyc', 'backup.pyc', 'mysql_manage.pyc', 'users.py', 'celeryHandle.py', 'assets.pyc', '__init__.pyc', 'ansible.py', '__init__.py', 'task_manage.pyc', 'cmdb.py', 'users.pyc', 'assets.py', 'system_manage.py', 'index.pyc', 'dict.pyc', 'backup.py']对当前目录下的文件存入列表>>> lists=os.listdir(os.getcwd())对列表进行排序>>> lists.sort()得到列表>>> print(lists)['__init__.py', '__init__.pyc', 'ansible.py', 'ansible.pyc', 'assets.py', 'assets.pyc', 'backup.py', 'backup.pyc', 'celeryHandle.py', 'cmdb.py', 'cmdb.pyc', 'cron.py', 'cron.pyc', 'deploy.py', 'deploy.pyc', 'dict.py', 'dict.pyc', 'index.py', 'index.pyc', 'mysql_manage.py', 'mysql_manage.pyc', 'sqlplan.py', 'system_manage.py', 'system_manage.pyc', 'task_manage.py', 'task_manage.pyc', 'tuning.ini', 'users.py', 'users.pyc']sort按key的关键字进行升序排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间,所以最终以文件时间从小到大排序最后对lists元素,按文件修改时间大小从小到大排序>>> lists.sort(key=lambda fn:os.path.getmtime(os.getcwd()+'/'+fn) )>>> print(lists)['__init__.py', 'deploy.py', 'cron.py', 'ansible.py', '__init__.pyc', 'cron.pyc', 'deploy.pyc', 'ansible.pyc', 'assets.py', 'assets.pyc', 'celeryHandle.py', 'sqlplan.py', 'tuning.ini', 'dict.py', 'dict.pyc', 'index.py', 'index.pyc', 'task_manage.py', 'task_manage.pyc', 'users.py', 'users.pyc', 'system_manage.py', 'system_manage.pyc', 'cmdb.py', 'cmdb.pyc', 'backup.py', 'backup.pyc', 'mysql_manage.py', 'mysql_manage.pyc']得到文件的扩展名,如果输入是文件夹,返回为空>>> os.path.splitext(os.getcwd())('/root/OpsManage-master/OpsManage/views', '')>>> os.path.splitext('/root/OpsManage-master/OpsManage/views/task_manage.pyc')('/root/OpsManage-master/OpsManage/views/task_manage', '.pyc')列出当前目录下所有的.py文件>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']['dict.py', 'sqlplan.py', 'task_manage.py', 'cron.py', 'mysql_manage.py', 'deploy.py', 'index.py', 'users.py', 'celeryHandle.py', 'ansible.py', '__init__.py', 'cmdb.py', 'assets.py', 'system_manage.py', 'backup.py']
数据结构操作
列表操作 >>> header=[1,2,3]>>> dat=[3,2,1]列表转换为字典>>> dict(zip(header,dat)){1: 3, 2: 2, 3: 1}运行操作系统命令,使用popen>>> cmd='hostname' >>> os.popen(cmd)>>> os.popen(cmd).read() 'dev01\n' 运行操作系统命令,使用commands,这个返回更丰富一些>>> import commands>>> commands.getstatusoutput('hostname')(0, 'dev01')列表的追加>>> ll=['a','b','c','d']>>> ll.append('jeanron100')>>> print(ll)['a', 'b', 'c', 'd', 'jeanron100']判断列表元素是否存在>>> print ll.count('jeanron100') 1 >>> print ll.count('jeanron1000') 0 列表的组合,如果是两个列表,效果就更清晰了>>> ll.extend(['jeanron','jianrong'])>>> print(ll)['a', 'b', 'c', 'd', 'jeanron100', 'jeanron', 'jianrong']删除指定元素>>> ll.remove('jeanron')>>> print(ll)['a', 'b', 'c', 'd', 'jeanron100', 'jianrong']反向输出列表元素>>> ll.reverse()>>> print(ll)['jianrong', 'jeanron100', 'd', 'c', 'b', 'a']列表排序>>> ll.sort()>>> print(ll)['a', 'b', 'c', 'd', 'jeanron100', 'jianrong']
字典操作 >>> info={'name':'jeanron','age':33,'gender':'male'}>>> print info.get('name')jeanron输出字典的键值>>> print info.keys()['gender', 'age', 'name']>>> print info.items()[('gender', 'male'), ('age', 33), ('name', 'jeanron')]以列表返回字典中的所有值>>> print info.values()['male', 33, 'jeanron']
集合操作 >>> info={'my','name','is','jeanron'}>>> print info set(['jeanron', 'is', 'my', 'name'])>>> test_info={'this','is','a','test'}集合交集>>> print info&test_info set(['is'])合集>>> print info.union(test_info) set(['a', 'name', 'this', 'is', 'jeanron', 'test', 'my'])并集>>> print info|test_info set(['a', 'name', 'this', 'is', 'jeanron', 'test', 'my'])
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
文件
路径
文件夹
元素
排序
字典
时间
基础
大小
目录
数据
数据结构
结构
从小到大
操作系统
从小
内容
命令
所在
文件名
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
联想服务器产品经理
数据库原理与应用 树状图
武汉基芯软件开发有限公司
钉钉数据库怎么整理
旅游类数据库有哪些字段
菲律宾机械硬盘服务器多少钱
维护网络安全 要抓住
互联网科技产业细分
南京招聘 软件开发
网络安全培训机构
浙江互联网科技企业排行榜
投资项目数据库app
四叶草学考网络技术
云备份恢复服务器异常
软件开发过程时间开销
c 备份数据库
怎么租云服务器架设cs服务器
30g数据库
数据库出生日期的有效性规则
信息系统 网络技术
浙江常规网络技术咨询怎么样
如何查询用户所在数据库
网络安全绘画手抄报简单内容
女生学网络技术专业好考吗
聊天软件用户数据库
云层服务器
男26岁转行做网络技术
你的文档数据库已经被加密
长龙网络技术服务有限公司
名师说计算机网络技术专业