亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2115 | 回復(fù): 2
打印 上一主題 下一主題

解惑:promgramming python 中的一段程序 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-11-26 21:10 |只看該作者 |倒序瀏覽
在programming python 中:講到這樣一個目錄拷貝的程序:哪 位高手能幫我解釋一下下面幾個問題:
1)程序中的verbose這個參數(shù)怎么用?為什么在開始定義了為global后,為0,而在后面的函數(shù)調(diào)用時,if條件還會成立?verbose  還會>1,或不為0嗎?
2)hasattr()這個method 有什么功能?是不是給指定參數(shù)賦一個attribute?
3)在 getargs()函數(shù)中,return ()在返回時有兩個參數(shù),那結(jié)果返回的是什么值?
程序如下:

[code]import os,sys
verbose=0import os,sys
verbose=0
dcount=fcount=0
maxfileload=500000
blksize=1024*100
def cpfile(pathfrom,pathto,maxfileload=maxfileload):
    """
    copy file pathfrom to pathto ,byte for byte
    """
    if os.path.getsize(pathfrom)<=maxfileload:
        bytesfrom=open(pathfrom,'rb').read()
        open(pathto,'wb').write(bytesfrom)
        filefrom=open(pathfrom,'rb')
        fileto =open(pathto,'wb')
        while 1:
            bytesfrom=filefrom.read(blksize)
            if not bytesfrom:break
            fileto.write(bytesfrom)
def cpall(dirfrom,dirto):
    """
    copy contents of dirfrom and below to dirto
    """
    global dcount ,fcount
    for file in os.listdir(dirfrom):
        pathfrom =os.path.join(dirfrom,file)
        pathto =os.path.join(dirto,file)
        if not os.path.isdir(pathfrom):
            try:
                if verbose >1:print 'copying',pathfrom,'to',pathto
                cpfile(pathfrom,pathto)
                fcount=fcount+1
            except:
                        print 'error creating',pathto,'--skipped'
                        print sys.exc_info()[0],sys.exc_info()[1]
            else:
                    if verbose:print 'copying dir',pathfrom,'to',pathto
                    try:
                        os.mkdir(pathto)
                        cpall(pathfrom,pathto)
                        dcount=dcount+1
                    except:
                        print 'error creating',pathto,'--skipped'
                        print sys.exc_info()[0],sys.exc_info()[1]

def getargs():
    try:
        dirfrom,dirto=sys.argv[1:]
    except:
        print 'use:cpall.py dirfrom dirto'
    else:
        if not os.path.isdir(dirfrom):
            print 'error :dirfrom is not a directory'
        elif not os.path.exists(dirto):
            os.mkdir(dirto)
            print 'note:dirto was created'
            return (dirfrom ,dirto)
        else:
            print 'warning:dirto alread exists'
            if dirfrom ==dirto or (hasattr(os.path, 'samefile')and
                                    os.path.samefile(dirfrom,dirto)):
                print 'error:dirfrom same as dirto'
            else:
                return (dirfrom ,dirto)
if __name__=='__main__':
    import time
    dirstuple=getargs()
    if dirstuple:
        print 'copying...'
        start=time.time()
        cpall(*dirstuple)
                                                                                             
print 'copied',fcount,'files,',dcount,'directories',
print 'in',time.time()-start,'seconds'


各位高手,請幫忙看一下吧。

論壇徽章:
0
2 [報告]
發(fā)表于 2010-11-27 07:37 |只看該作者
1. verbose 應(yīng)該只是用來調(diào)試用的吧

2. hasattr = has attribute ? = 這個 object 有沒有這個 method
  1. hasattr(...)
  2.     hasattr(object, name) -> bool
  3.    
  4.     Return whether the object has an attribute with the given name.
  5.     (This is done by calling getattr(object, name) and catching exceptions.)
復(fù)制代碼
3. tuple

論壇徽章:
0
3 [報告]
發(fā)表于 2010-11-27 19:58 |只看該作者
明白了,多謝2樓指點。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP