解惑: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'
各位高手,请帮忙看一下吧。
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
复制代码
3. tuple
2. hasattr = has attribute ? = 這個 object 有沒有這個 method
- hasattr(...)
- hasattr(object, name) -> bool
-
- Return whether the object has an attribute with the given name.
- (This is done by calling getattr(object, name) and catching exceptions.)
作者: eookoo 发布时间: 2010-11-27
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28