歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 如何用Python過濾顯示文件或者文件夾

如何用Python過濾顯示文件或者文件夾

日期:2017/2/28 16:03:10   编辑:Linux教程

利用Python的os模塊可以很方便的顯示過濾文件和文件夾,下面來看下演示,直接看代碼:

[[email protected] ~]# 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的功能很強大,值得我們來學習和掌握,希望能對你的學習和工作有幫助。

Copyright © Linux教程網 All Rights Reserved