How to think like a Computer Scientist: 课后习题第十八章9-11

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     19/09/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import os
import sys

def recursion_depth(number):
    #print " Current recurion depth is {0}".format(number)
    if number == 0:
        return
    recursion_depth(number - 1)


def get_filepath(path):
    '''
    Walk a directory structure and return a list of all the full
paths of files in the directory or the subdirctories
    '''
    file_list = []
    dir_list = os.listdir(path)
    dir_list.sort()

    for fl in dir_list:
        fullname = os.path.join(path, fl)
        if os.path.isdir(fullname):
            file_list += get_filepath(fullname)
        else:
            file_list.append(fullname)

    return file_list

def litter(path = None):
    '''
    create an empty file name trash.txt in each subdirectory of a direcotry tree
    given the root of the tree as an argument or the current direcotry as default
    '''
    if path == None:
        path = os.getcwd()
    dir_list = os.listdir(path)
    dir_list.sort()

    for fl in dir_list:
        fullname = os.path.join(path, fl)
        if os.path.isdir(fullname):
            litter(fullname)

    new_Path = os.path.join(path, 'trash.txt')
    print new_Path
    new_file = open(new_Path, 'w')
    new_file.close()

    return

def cleanup(path = None):
    '''
    remove all these files created in litter function
    '''
    if path == None:
        path = os.getcwd()

    dir_list = os.listdir(path)
    dir_list.sort()

    for fl in dir_list:
        fullname = os.path.join(path, fl)
        if os.path.isdir(fullname):
            cleanup(fullname)

    rm_filepath = os.path.join(path, 'trash.txt')
    if os.path.isfile(rm_filepath):
        print rm_filepath
        os.unlink(rm_filepath)

    return

def main():

    sys.setrecursionlimit(100)
    print "Get current recursion depth is : ", sys.getrecursionlimit()

    recursion_depth(64)

    '''
    next function will fail,so comment it
    It is based on the TOTAL stack depth and not really the depth of any particular single function. You are probably already at a stack depth of 5 when you make the first call to rec().

    Take for example 5 recursive functions. Each makes 98 recursive calls with the last one being to the next recursive function. With a recursion limit of 100 do you really want to allow each recursive function to make 99 calls for a total depth of ~500 calls? No, that might crash the interpreter at those depths.

    Therefore the recursion limit is the maximum depth of all functions globally, not any single named function.
    '''

    #recursion_depth(65)

    print get_filepath(r'D:\tcl')
    litter (r'D:\tcl')
    cleanup(r'D:\tcl')

    litter()
    cleanup()
    pass

if __name__ == '__main__':
    main()
>>> 
*** Remote Interpreter Reinitialized  ***
>>> 
Get current recursion depth is :  100
['D:\\tcl\\tcl85.lib', 'D:\\tcl\\tclConfig.sh', 'D:\\tcl\\tclstub85.lib', 'D:\\tcl\\tix8.4.3\\Balloon.tcl', 'D:\\tcl\\tix8.4.3\\BtnBox.tcl', 'D:\\tcl\\tix8.4.3\\ChkList.tcl', 'D:\\tcl\\tix8.4.3\\bitmaps\\act_fold.gif', 'D:\\tcl\\tix8.4.3\\bitmaps\\act_fold.xbm', 'D:\\tcl\\tix8.4.3\\demos\\MkChoose.tcl', 'D:\\tcl\\tix8.4.3\\demos\\MkDirLis.tcl', 'D:\\tcl\\tix8.4.3\\demos\\MkSample.tcl', 'D:\\tcl\\tix8.4.3\\demos\\MkScroll.tcl', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\about.xpm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\bold.xbm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\capital.xbm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\centerj.xbm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\code.xpm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\combobox.xbm', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\tix.gif', 'D:\\tcl\\tix8.4.3\\demos\\bitmaps\\underlin.xbm', 'D:\\tcl\\tix8.4.3\\demos\\samples\\AllSampl.tcl', 'D:\\tcl\\tix8.4.3\\demos\\samples\\ArrowBtn.tcl', 'D:\\tcl\\tix8.4.3\\demos\\samples\\Balloon.tcl', 'D:\\tcl\\tix8.4.3\\demos\\tclIndex', 'D:\\tcl\\tix8.4.3\\demos\\tixwidgets.tcl', 'D:\\tcl\\tix8.4.3\\demos\\widget', 'D:\\tcl\\tix8.4.3\\pref\\10Point.fs', 'D:\\tcl\\tix8.4.3\\pref\\10Point.fsc', 'D:\\tcl\\tix8.4.3\\pref\\12Point.fs', 'D:\\tcl\\tk8.5\\bgerror.tcl', 'D:\\tcl\\tk8.5\\button.tcl', 'D:\\tcl\\tk8.5\\choosedir.tcl', 'D:\\tcl\\tk8.5\\clrpick.tcl', 'D:\\tcl\\tk8.5\\demos\\anilabel.tcl', 'D:\\tcl\\tk8.5\\demos\\aniwave.tcl', 'D:\\tcl\\tk8.5\\demos\\arrow.tcl', 'D:\\tcl\\tk8.5\\demos\\bind.tcl', 'D:\\tcl\\tk8.5\\demos\\images\\earth.gif', 'D:\\tcl\\tk8.5\\demos\\images\\earthris.gif', 'D:\\tcl\\tk8.5\\demos\\images\\face.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\flagdown.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\flagup.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\gray25.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\letters.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\noletter.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\pattern.xbm', 'D:\\tcl\\tk8.5\\demos\\images\\tcllogo.gif', 'D:\\tcl\\tk8.5\\demos\\images\\teapot.ppm', 'D:\\tcl\\tk8.5\\images\\logo.eps', 'D:\\tcl\\tk8.5\\images\\logo100.gif', 'D:\\tcl\\tk8.5\\images\\logo64.gif', 'D:\\tcl\\tk8.5\\images\\logoLarge.gif', 'D:\\tcl\\tk8.5\\images\\logoMed.gif', 'D:\\tcl\\tk8.5\\images\\pwrdLogo.eps', 'D:\\tcl\\tk8.5\\msgs\\cs.msg', 'D:\\tcl\\tk8.5\\msgs\\da.msg', 'D:\\tcl\\tk8.5\\msgs\\de.msg', 'D:\\tcl\\tk8.5\\ttk\\altTheme.tcl', 'D:\\tcl\\tk8.5\\ttk\\aquaTheme.tcl', 'D:\\tcl\\tk85.lib', 'D:\\tcl\\tkstub85.lib']
D:\tcl\tix8.4.3\bitmaps\trash.txt
D:\tcl\tix8.4.3\demos\bitmaps\trash.txt
D:\tcl\tix8.4.3\demos\samples\trash.txt
D:\tcl\tix8.4.3\demos\trash.txt
D:\tcl\tix8.4.3\pref\trash.txt
D:\tcl\tix8.4.3\trash.txt
D:\tcl\tk8.5\demos\images\trash.txt
D:\tcl\tk8.5\demos\trash.txt
D:\tcl\tk8.5\images\trash.txt
D:\tcl\tk8.5\msgs\trash.txt
D:\tcl\tk8.5\ttk\trash.txt
D:\tcl\tk8.5\trash.txt
D:\tcl\trash.txt
D:\tcl\tix8.4.3\bitmaps\trash.txt
D:\tcl\tix8.4.3\demos\bitmaps\trash.txt
D:\tcl\tix8.4.3\demos\samples\trash.txt
D:\tcl\tix8.4.3\demos\trash.txt
D:\tcl\tix8.4.3\pref\trash.txt
D:\tcl\tix8.4.3\trash.txt
D:\tcl\tk8.5\demos\images\trash.txt
D:\tcl\tk8.5\demos\trash.txt
D:\tcl\tk8.5\images\trash.txt
D:\tcl\tk8.5\msgs\trash.txt
D:\tcl\tk8.5\ttk\trash.txt
D:\tcl\tk8.5\trash.txt
D:\tcl\trash.txt
C:\Users\penglaixy\Desktop\python exciese\trash.txt
C:\Users\penglaixy\Desktop\python exciese\trash.txt


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值