千家信息网

Python基础中os和数据结构是怎么样的

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,Python基础中os和数据结构是怎么样的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。今天总结了下Python的基础,
千家信息网最后更新 2026年11月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安全错误 数据库的锁怎样保障安全 互联网科技公司简介 广东猪兼强互联网科技有限公司 互联网科技上市公司 互联网女皇 腾讯科技 广州凡科互联网科技 亿人帮互联网科技 互联网之后的科技是什么 小米互联网科技园设置 湖南诚度互联网科技有限公司 受益于互联网科技的高速发展 湖北码尼互联网科技有限公司电话 知域互联网科技有限公司大骗子 宜昌元轩互联网科技有限公司 银川新华互联网科技学校联系电话 互联网保险科技创业 互联网方面的科技前沿 互联网领先科技宣传片 张召忠谈中国互联网科技 神州科技互联网有限公司 北京军民互联网科技公司 互联网巨头科技公司 新浪科技互联网 深圳期待互联网科技 汉立互联网科技有限公司联系电话 科技互联网的估值 中国互联网有什么科技含量 成都铂石互联网科技做什么的 互联网金融中的现代科技 互联网科技大奖都有哪些公司 世界互联网领先科技成果获奖
0