概述
利用python的os模块可以很方便的显示过滤文件和文件夹,下面来看下演示,直接看代码:
[root@centos6 ~]# ipython
Python 2.6.5 (r265:79063, Nov 12 2010, 00:45:29)
Type "copyright", "credits" or "license" for more information.
IPython 0.10.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: file_list=!ls
/usr/lib/python2.6/site-packages/IPython/genutils.py:437: DeprecationWarning: os.popen3 is deprecated. Use the subprocess module.
pin,pout,perr = os.popen3(cmd)
In [2]: import os 导入模块
In [3]: file_list=!ls #新建序列
In [4]: cd /root #切换路径
/etc
In [5]: file_list.grep(os.path.isfile) #显示文件
Out[5]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: anaconda-ks.cfg
1: bwm-ng-0.6.tar.gz
2: ez.py
3: install.log
4: install.log.syslog
In [6]: file_list.grep(os.path.isdir) #显示目录
Out[6]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: search
1: search1
In [7]:
Python 2.6.5 (r265:79063, Nov 12 2010, 00:45:29)
Type "copyright", "credits" or "license" for more information.
IPython 0.10.2 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: file_list=!ls
/usr/lib/python2.6/site-packages/IPython/genutils.py:437: DeprecationWarning: os.popen3 is deprecated. Use the subprocess module.
pin,pout,perr = os.popen3(cmd)
In [2]: import os 导入模块
In [3]: file_list=!ls #新建序列
In [4]: cd /root #切换路径
/etc
In [5]: file_list.grep(os.path.isfile) #显示文件
Out[5]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: anaconda-ks.cfg
1: bwm-ng-0.6.tar.gz
2: ez.py
3: install.log
4: install.log.syslog
In [6]: file_list.grep(os.path.isdir) #显示目录
Out[6]: SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):
0: search
1: search1
In [7]:
python的功能很强大,值得我们来学习和掌握,希望能对你的学习和工作有帮助。。。
本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/627368如需转载请自行联系原作者
world77
最后
以上就是聪明背包为你收集整理的如何用python过滤显示文件或者文件夹的全部内容,希望文章能够帮你解决如何用python过滤显示文件或者文件夹所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复