千家信息网

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'])

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

0