Python遞迴列出目錄中檔案指令碼及其匿名函數

來源:互聯網
上載者:User

標籤:/etc   對象   ems   去掉   需要   module   檔案的   優點   ace   

1.遞迴列出目錄裡的檔案的指令碼舉例

    列出目錄中的檔案可以通過下面方法:os.listdir()

    In [1]: import os

    In [4]: os.listdir('/root')

    Out[4]:

    ['.tcshrc',

    '.bash_history',

    '.bashrc',

    'ENV',

    '.cache',

    '.config',

    '.cshrc',

    '.bash_logout',

    'python',

    '.ssh',

    'shell',

    '.bash_profile',

    '.ipython',

    '.viminfo',

    'dictionary.txt',

    '1.txt']

    

判斷是否為目錄:

    In [5]: os.path.isdir('/home')

    Out[5]: True


判斷是否為檔案:

    In [7]: os.path.isfile('/etc/rc.local')

    Out[7]: True


拼接檔案名稱字的絕對路徑:

    In [8]: os.path.join('/etc/','passwd','abc')

    Out[8]: '/etc/passwd/abc'


列出目錄下所有檔案指令碼如果下:

    #!/usr/bin/env python

    # @Time     :2018-01-05 15:11

    # @Author   :FengXiaoqing

    # @file     :listDir.py

    import os

    import sys

    def print_files(path):

        lsdir = os.listdir(path)

        dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i))]

        files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]

        if dirs:

            for d in dirs:

                print_files(os.path.join(path,d))

        if files:

            for f in files:

                print os.path.join(path,f)

    print_files(sys.argv[1])


2.匿名函數lambda

    lambda函數是一種快速定義單選的最小函數,可以用在任何需要函數的地方。

    3*5實現方法:

    In [1]: def fun(x,y):

    ...:     return x * y

    ...:

    In [2]: fun(3,5)

    Out[2]: 15


匿名函數定義如果下:

    In [3]: lambda x,y:x * y

    Out[3]: <function __main__.<lambda>>    #返回的對象

    In [4]: r = lambda x,y:x * y

    In [6]: r(3,5)

    Out[6]: 15


匿名函數優點:

    1.使用python寫一些指令碼時候,使用lambda可以省去定義函數的過程,讓代碼更加精簡。

    2.對於一些抽象的,不會被別的地方再重複使用的函數,有時候函數起個名字也是個難題,使用lambda不需要層次理論考慮命名的問題。

    3.使用lambda在某些時候讓代碼更容易理解。


lambda基礎:

    lambda語句中,冒號前是參數,可以有多個,逗號隔開,冒號右邊是傳回值。

    lambda語句構建的其實是一個函數對象。


help(reduce)

    Help on built-in function reduce in module __builtin__:

    reduce(...)

    reduce(function, sequence[, initial]) -> value

    Apply a function of two arguments cumulatively to the items of a sequence,

    from left to right, so as to reduce the sequence to a single value.

    For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

    ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items

    of the sequence in the calculation, and serves as a default when the

    sequence is empty.

    (END)


reduce二元計算:

    In [19]: def add(x,y):

    return x + y

    ....:

    In [20]: add(1,3)

    Out[20]: 4


求1到100相加的和:

    In [23]: reduce(add,range(1,101))

    Out[23]: 5050


    In [25]: reduce(lambda x,y:x + y ,range(1,101))

    Out[25]: 5050


求階乘:

    In [26]: reduce(lambda x,y:x * y ,range(1,6))

    Out[26]: 120


習題

    1. 從終端接收若干個數字,要求使用filter()函數,將輸入的不是數位值剔除掉(使用者輸入的內容有隨機性,當我們要接收一個數位時候,他可能會輸入一個字串過來,要求當使用者輸入的不是數字,就剔除掉)


    2. 從終端接收若干個以空格隔開的字串,然後去除所有的26個字元之外的字元後,列印到螢幕上

    要求:使用map()函數,map()函數接收兩個參數,一個是函數,一個是Iterable,map將傳入的函數依次作用到序列的每個元素,並把結果作為新的Iterator返回。


    3.從終端接收若干個以空格隔開的字串

    (1).以空格隔開的字串為使用者想要輸入的一個值

    (2).使用者在輸入時不小心按出來了字母鍵

    樣本如下

    "afda111 dafd222 3dfad33 4ss4dd4"

    這個字串,其實是使用者想要求 111 222 333 444 的和

    要求:把每個值中的字母去掉,並計算出所有值的和

提示:使用reduce函數,結合前兩題使用過的方法,可以很簡單的算出


Python遞迴列出目錄中檔案指令碼及其匿名函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.