前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基础之os和数据结构

Python基础之os和数据结构

作者头像
jeanron100
发布2018-03-22 15:28:45
8160
发布2018-03-22 15:28:45
举报

今天总结了下Python的基础,发现还是有很多基础需要巩固,直接把学习的内容放上来。

代码语言:javascript
复制
>>> 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
代码语言:javascript
复制
一些路径在不同操作平台的表示
>>> os.sep
'/'
>>> os.extsep
'.'
>>> os.linesep
'\n'
>>> os.pathsep
':'
代码语言:javascript
复制
得到目录下的文件
>>> 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']

数据结构操作

代码语言:javascript
复制
列表操作
>>> header=[1,2,3]
>>> dat=[3,2,1]
列表转换为字典
>>> dict(zip(header,dat))
{1: 3, 2: 2, 3: 1}
运行操作系统命令,使用popen
>>> cmd='hostname'
>>> os.popen(cmd)
<open file 'hostname', mode 'r' at 0x7f416e1d45d0>
>>> 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']
代码语言:javascript
复制
字典操作
>>> 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']
代码语言:javascript
复制
集合操作
>>> 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'])
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-03-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 杨建荣的学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档