Python基础中os和数据结构是怎么样的
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,Python基础中os和数据结构是怎么样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。今天总结了下Python的基础,
千家信息网最后更新 2025年12月02日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安全错误
数据库的锁怎样保障安全
网络安全协议保密性
山东嵌入式软件开发公司电话
软件开发公司一般几点上班
亚马逊开店需要什么服务器
2核8g服务器
服务器上打开pdf
思科网络安全第五章考试答案
中国标准文献数据库
义乌市网络安全应急指挥中心大楼
地下城手游正在检查服务器-9
使命召唤16不同服务器可以玩吗
关系数据库导入hive
数据库开发liferay
女性网络安全手册漫画
网络技术一个月能挣多少钱
数据库表的主键外键有什么作用
亿联网络技术竞争力
数据库查询开发者
dal 层数据库连接代码
老板不懂软件开发技术
王者荣耀安卓服务器哪个好
数据库和服务器不符合怎么办
燃烧的远征霜语服务器拍卖行在哪
数据库文件的后缀名是dbf
软件开发年终总结个计划
腾飞行情软件开发商
电脑有线宽带无法连接远程服务器
恩商网络技术有限公司
适合pve服务器的职业
emcc添加数据库监控