python代码整理

1328阅读 0评论2010-12-26 hkebao
分类:Python/Ruby

#实现将某个目录下面的py后缀名的文件修改成asp

import os,string
def s_rename(path,old_ext,new_ext):
     for (path, dirs, files) in os.walk(path):
        for filename in files:
             ext=os.path.splitext(filename)[1]
             if (cmp(ext,old_ext)==0):
                 newname=filename.replace(old_ext,new_ext)
                 oldpath=path+"\\"+filename
                 newpath=path+"\\"+newname
                 print "oldpath:"+oldpath+""
                 print "newpth:"+newpath+""
                 try:
                     os.rename(oldpath, newpath)
                 except ValueError:
                     print "Error when rename the file " + oldpath
                 except NameError:
                     print "Error when rename the file " + oldpath
                 except OSError:
                     #print OSError
                     print newpath + " The file is already exist!"
if __name__ == '__main__':
     s_rename("E:\\func",".py",".asp")



import os,string  
def Denary2Binary(n):
    bStr = ''
    if n < 0: raise ValueError, "must be a positive integer"
    if n == 0: return '0'
    while n > 0:
        bStr = str(n % 2) + bStr
        n = n >> 1
    return bStr


if __name__ == '__main__':  
     print Denary2Binary(8)
     print int('10',2)
上一篇:Puppet英文翻译整理资料[翻译三]
下一篇:Java常用函数封装[原创]