+ -
当前位置:首页 → 问答吧 → 解惑:promgramming python 中的一段程序

解惑:promgramming python 中的一段程序

时间:2010-11-26

来源:互联网

在programming python 中:讲到这样一个目录拷贝的程序:哪 位高手能帮我解释一下下面几个问题:
1)程序中的verbose这个参数怎么用?为什么在开始定义了为global后,为0,而在后面的函数调用时,if条件还会成立?verbose  还会>1,或不为0吗?
2)hasattr()这个method 有什么功能?是不是给指定参数赋一个attribute?
3)在 getargs()函数中,return ()在返回时有两个参数,那结果返回的是什么值?
程序如下:

[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'


各位高手,请帮忙看一下吧。

作者: vividvivion   发布时间: 2010-11-26

1. verbose 應該只是用來調試用的吧

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.)
复制代码
3. tuple

作者: eookoo   发布时间: 2010-11-27