如何用python过滤显示文件或者文件夹
2011-07-30 22:21
465 查看
利用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的功能很强大,值得我们来学习和掌握,希望能对你的学习和工作有帮助。。。
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的功能很强大,值得我们来学习和掌握,希望能对你的学习和工作有帮助。。。
相关文章推荐
- cmd 如何打开当前文件夹或者文件
- Git如何Check Out出指定文件或者文件夹
- Mac下如何显示隐藏文件/文件夹
- c#如何监视文件或者文件夹的变化
- [ubuntu]如何跨机器拷贝文件或者文件夹
- Mac如何让 Finder 显示隐藏文件和文件夹
- Git如何Check Out出指定文件或者文件夹
- SVN提交时如何过滤文件和文件夹
- 如何让 Finder 显示隐藏文件和文件夹
- Python下使用help(dict),显示'more'不是内部或外部命令,也不是可运行的程序或批处理文件,该如何处理?
- python 在excel文件中写入date日期数据,以及读取excel日期数据,如何在python中正确显示date日期。
- 算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
- 如何解决java文件上面有错,但是文件夹上面不显示的错误
- phpstorm如何进行文件或者文件夹重命名
- 如何将服务器端文件 自动下载并保存到客户端IE临时文件夹 或者指定文件夹
- python:如何将txt文件中的数值数据读入到list中,且在list中存在的格式为float类型或者其他数值类型
- python如何获取一个文件夹内包括子文件夹所有文件的名字和路径
- 算法积累:解决如何获取指定文件夹路径或者文件路径下所有子文件后缀为.h .m .c的文本的行数
- VS2010中,如何在“解决方案资源管理器”中显示从别处复制过来的文件夹或文件
- 如何使用Python 打开exel等各种文件或者应用,就用:os.startfile