ITeye博客

用python找出最近修改的文件

全冠清 · 2010-09-17 19:26 · 编程语言 · CSS · OS · JSP · Python · SVN · Markdown

上线时几个人同时过上百个文件,这时就需要列出修改过文件的列表 手工记录几乎不可能,这时可以用文件最后修改时间和开始修改时的时间做比较,这时就可以对比出列表


import os,os.path,re,time

filelist=[]
path = 'F:\hbtv'

t=time.strptime("2010-09-15 00:00:00", "%Y-%m-%d %H:%M:%S")
t= time.mktime(t)



for root, dirs, files in os.walk(path):
    for file in files:
        path= os.path.join(root, file)
        if (not re.match(r".*(\.svn|\.project|html\.\d+|Thumbs\.db).*", path)):# and os.path.getmtime(file)>t :
            filelist.append(path)

for i in filelist:
  if os.path.getmtime(i)>t:
      print i#,time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(os.path.getmtime(i)))

评论(8)

  1. #8 楼 · 全冠清 · 2011-07-25 15:15
    
    #!/usr/local/bin/python
    
    #coding=utf-8
    
    "change images path"
    
    import os,os.path,re,sys,shutil
    
    
    def changepath(path,rules):
        types=[key for key in rules]
        for root,dirs,files in os.walk(path):
            for file in files:
                for i in rules:
                    if file.endswith(i):
                       page=open(os.path.join(root,file)).read()
                       if re.search(r'\.jpg|\.gif|\.png|\.css', page):
                          for i in rules[i]:
                              if len(i)==3:
                                  page=re.sub(i[0], i[1],page)
                              else:
                                  page=page.replace(*i)
                          open(os.path.join(root,file),'w').write(page)
                          print os.path.join(root,file)
    
    def copychanges(src,target,changelists):
           if not os.path.exists(target):
                     os.makedirs(target)
           j=0
           nl=[]
           for i in changelists:
                 l= os.path.split(i)
                 if not os.path.exists(os.path.join(target,l[0])):
                     os.makedirs(os.path.join(target,l[0]))
                 try:    
                     shutil.copyfile(os.path.join(src,i), os.path.join(target,i))
                 except Exception,e:
                      print e
                      j=j+1
                      print j
                      nl.append(i.strip())
           print "#" *100
           print "uncopy file:"
           for i in nl:
               print i
           print "%s%s%s" %('total',len(nl),'')       
    
    def getStaticMoveList(source):
              l=[]
              for root,dirs,files in os.walk(source):
                    for file in files:
                        l.append(os.path.join(root,file))
              l=[i.replace(source,'')[1:] for i in l if i[-3:] not in ['jsp','ass','xml','ftl']]      
              return l 
    
    if __name__=='__main__':
         rules={".jsp":[ (r'http://image21.19e.cn:18060/',r'http://image30.19360.cn/'),
                       (r'url(/',r'url(http://image30.19360.cn/transaction3.0/'),
                       (r'src="/(?=(\w|\.|_|/)+?(\.gif"|\.png"|\.jpg"|\.css"))',r'src="http://image30.19360.cn/transaction3.0/',True),
                       (r"src='/(?=(\w|\.|_|/)+?(\.gif'|\.png'|\.jpg'|\.css'))",r"src='http://image30.19360.cn/transaction3.0/",True),
                        (r"href='/(?=(\w|\.|_|/)+?\.css')",r"href='http://image30.19360.cn/transaction3.0/",True),
                         (r'href="/(?=(\w|\.|_|/)+?\.css")',r'href="http://image30.19360.cn/transaction3.0/',True)],
           ".html":[ (r'http://image21.19e.cn:18060/',r'http://image30.19360.cn/'), 
                       (r'url(/',r'url(http://image30.19360.cn/transaction3.0/'),
                       (r'src="/(?=(\w|\.|_|/)+?(\.gif"|\.png"|\.jpg"|\.css"))',r'src="http://image30.19360.cn/transaction3.0/',True),
                       (r"src='/(?=(\w|\.|_|/)+?(\.gif'|\.png'|\.jpg'|\.css'))",r"src='http://image30.19360.cn/transaction3.0/",True),
                       (r"href='/(?=(\w|\.|_|/)+?\.css')",r"href='http://image30.19360.cn/transaction3.0/",True),
                         (r'href="/(?=(\w|\.|_|/)+?\.css")',r'href="http://image30.19360.cn/transaction3.0/',True)],     
            ".htm":[ (r'http://image21.19e.cn:18060/',r'http://image30.19360.cn/'),
                       (r'url(/',r'url(http://image30.19360.cn/transaction3.0/'),
                       (r'src="/(?=(\w|\.|_|/)+?(\.gif"|\.png"|\.jpg"|\.css"))',r'src="http://image30.19360.cn/transaction3.0/',True),
                       (r"src='/(?=(\w|\.|_|/)+?(\.gif'|\.png'|\.jpg'|\.css'))",r"src='http://image30.19360.cn/transaction3.0/",True),
                       (r"href='/(?=(\w|\.|_|/)+?\.css')",r"href='http://image30.19360.cn/transaction3.0/",True),
                       (r'href="/(?=(\w|\.|_|/)+?\.css")',r'href="http://image30.19360.cn/transaction3.0/',True)],                  
           ".css":[ (r'http://image21.19e.cn:18060/',r'http://image30.19360.cn/'),
                       (r'url(/',r'url(http://image30.19360.cn/transaction3.0/')]}
         for arg  in sys.argv:
               print arg            
         if len(sys.argv) < 2:
              print "require path"
         else:    
              path=sys.argv[1]
              path=os.path.abspath(path)
              changepath(path,rules)
              l=getStaticMoveList(path)   
              copychanges(path,"%s%s"%(path,"_static"),l)
    
    
  2. #7 楼 · 全冠清 · 2011-05-27 09:49

    检测空链接

    
    def validateUrl(url):
            up=urlparse.urlparse(url)
            http=httplib.HTTP(up[1])
            http.putrequest('GET',up[2])
            http.putheader('Accept','*/*')
            http.endheaders()
            errcode,errmsg,headers=http.getreply()
            if errcode==200:
                return False
            else:
                return True
     
    
    
  3. #6 楼 · 全冠清 · 2011-05-27 09:47

    静态文件加参数

    
    #!/usr/bin/python
    #coding=utf-8
     
    """静态文件加参数"""
     
    import os.path
     
    def main(path,rules,filerules):
        for root,dirs,files in os.walk(path):
            for file in files:
                if filerules(file):
                    page=open(os.path.join(root,file)).read()
                    for i in rules:
                        page=page.replace(*i)
                        open(os.path.join(root,file),'w').write(page)
                        print os.path.join(root,file)
     
    if __name__=='__main__':
        rules=[(r'.%s%s'%(i,j),r'.%s%s%s'%(i,'?20110527',j)) for i in ['css','jpg','gif','png'] for j in ['"',"'",')']]
        filerules=lambda x: x.endswith(".html") or x.endswith(".htm") or x.endswith(".jsp") or x.endswith(".css")
        path="D:\\temp"
        main(path,rules,filerules)
    
    
  4. #5 楼 · 全冠清 · 2011-05-18 17:45

    批量拷贝变更

    
    import os,os.path,shutil
     
    def copychanges(src,target,changelists):
           j=0
           for i in changelists:
                 if i[-5:] ==".java":
                     i="%s%s"%(i[:-5],".class")
                 l= os.path.split(i)
                 if not os.path.exists("%s%s"%(target,l[0])):
                     os.makedirs("%s%s"%(target,l[0]))
                 try:   
                     shutil.copyfile("%s%s"%(src,i.strip()), "%s%s"%(target,i.strip()))
                     print "%s%s"%(target,i.strip())
                 except Exception:
                      j=j+1
                      print j   
    
    
  5. #4 楼 · 全冠清 · 2011-03-25 13:46
    
    #!/usr/bin/python
    #coding=gb2312
    
    import urllib2,cookielib,urllib,re,time,threading
    
    class Conn(object):
        '''
        classdocs
        '''
    
    
        def __init__(self):
                cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
                opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
                urllib2.install_opener(opener)
                self.headers = {
                                            'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
                                        }
        
        def request(self,url,data={}):
               postdata=urllib.urlencode(data)      
               req = urllib2.Request(
                                                   url =url,
                                                   data = postdata,
                                                   headers=self.headers
                                                   )
               link=urllib2.urlopen(req)
               result = link.read()
               link.close()
               return  result
    
    c=Conn()
    
    def login():
            login_page= c.request('http://www.517na.cn/login.aspx')
            imgdata=urllib2.urlopen("http://www.517na.cn/Images/ValidateCode.aspx").read()
            f=open("img.gif","wb")
            f.write(imgdata)
            f.close()
            v_code=raw_input("请输入验证码内容,按回车键结束:")
            login_page=c.request('http://www.517na.cn/login.aspx?ReturnUrl=%2fDefault.aspx',{
                                                                                                                                      'ibtnLogin.x':   17,
                                                                                                                                    'ibtnLogin.y':    33,
                                                                                                                                    'txtPWD':'xxx',
                                                                                                                                    'txtUser':'xxx',
                                                                                                                                    'txtValied':v_code.strip()[:4],
                                                                                                                                    '__VIEWSTATE':re.findall(r'id="__VIEWSTATE" value="(.+?)"', login_page)[0],
                                                                                                                                    '__EVENTVALIDATION':re.findall(r'id="__EVENTVALIDATION" value="(.+?)"', login_page)[0]
                                                                                                                                })
            if re.search(r'BullShow',login_page):
                print '登录成功'
            else:
                print '登录失败,重新登录'
                login()
    
    tr_pattern=re.compile(r'<tr class="hjtr(2|3)".+?>(.+?)</tr>', re.S)
    td_pattern=re.compile(r'<td>(.*?)</td>',re.S)
    vstate_pattern=re.compile(r'id="__VIEWSTATE" value="(.+?)"')
    
    def getData(airCode,fr,to):
            page=c.request('http://www.517na.cn/CaiGou/PolicyShow.aspx')
            page=c.request('http://www.517na.cn/CaiGou/PolicyShow.aspx',{
                                                                                                                     '__VIEWSTATE':vstate_pattern.findall(page)[0],
                                                                                                                   'BtnQuery' :'查询',
                                                                                                                      'DropAircoCode':   airCode,
                                                                                                                    'FromCity$dropCity':    fr,
                                                                                                                    'FromCity$txtCity':   fr,
                                                                                                                    'ToCity$dropCity' :   to,
                                                                                                                    'ToCity$txtCity '  : to,
                                                                                                                    'selPolicyType':    '0',
                                                                                                                    'txtTime':    time.strftime("%Y-%m-%d", time.localtime())
                                                                                                                })
            l=tr_pattern.findall( page)
            l=[i[1] for i in l]
            l=[td_pattern.findall(i) for i in l]
            return l
    
    def t_getData(airCode,airports):
            for j in airports:
                    for k in airports[:]:
                          l=getData(airCode,j,k)
                          for i in l:
                                print i
    if __name__ == '__main__':
            airCompanys=['3u', '8c', '8l', '9c', 'ab', 'bk', 'ca', 'cn', 'cz', 'eu', 'fm', 'g5', 'gs', 'ho', 'hu', 'jd', 'jr', 'kn', 'ky', 'mf', 'mu', 'ns', 'pn', 'sc', 'vd', 'ya', 'zh']
            airports=['ala', 'aku', 'aka', 'aat', 'mfm', 'aqg', 'ava', 'aog', 'ayn', 'bfu', 'bhy', 'nay', 'pek', 'bpl', 'aeb', 'bsd', 'bav', 'nbs', 'cgq', 'bpx', 'cgd', 'ctu', 'cif', 'cni', 'ckg', 'csx', 'chg', 'cih', 'czx', 'ccc', 'ddg', 'dnh', 'dlu', 'dlc', 'dqa', 'dig', 'dat', 'doy', 'dax', 'dzu', 'dsn', 'enh', 'fuo', 'fug', 'fyn', 'foc', 'goq', 'ghn', 'kwl', 'khh', 'gyu', 'gys', 'kwe', 'kow', 'can', 'hia', 'hdg', 'hrb', 'hfe', 'het', 'hek', 'hjj', 'hak', 'hld', 'hun', 'hmi', 'txn', 'htn', 'hny', 'hzg', 'hgh', 'huz', 'knc', 'jdz', 'jgs', 'jhg', 'jjn', 'jiu', 'jil', 'jmu', 'knh', 'tna', 'jng', 'chw', 'jxa', 'jgn', 'cyi', 'jzh', 'cju', 'jnz', 'shs', 'kca', 'kgt', 'krl', 'kry', 'kmg', 'kji', 'khg', 'llb', 'lcx', 'lnj', 'ljg', 'hzh', 'lia', 'lxa', 'lhn', 'luz', 'lxi', 'lyg', 'kyd', 'lyi', 'lya', 'lhw', 'lzy', 'lzh', 'lzo', 'mdg', 'mzg', 'ohe', 'lum', 'mig', 'nzh', 'lzn', 'mfk', 'mxz', 'ngb', 'khn', 'nao', 'nkg', 'nlt', 'erl', 'nng', 'ntg', 'nny', 'hcn', 'pif', 'cmj', 'wot', 'pzi', 'tao', 'shp', 'iqm', 'ndg', 'iqn', 'hin', 'juz', 'pvg', 'sha', 'sjw', 'sym', 'swa', 'syx', 'she', 'szx', 'szv', 'tsa', 'tpe', 'tcg', 'tcz', 'gni', 'ttt', 'tnh', 'tsn', 'tgo', 'tnn', 'ten', 'tvs', 'thq', 'tyn', 'rmq', 'txg', 'hyn', 'wef', 'weh', 'wua', 'whu', 'wuh', 'hlh', 'urc', 'wnh', 'wux', 'wus', 'wxn', 'wnz', 'wuz', 'xiy', 'xic', 'xen', 'xfn', 'hkg', 'xil', 'xmn', 'xnn', 'xin', 'xnt', 'acx', 'xuz', 'eny', 'ybp', 'ynz', 'lds', 'yih', 'inc', 'ycu', 'ynj', 'yln', 'uyn', 'yua', 'yin', 'yus', 'ynt', 'yiw', 'llf', 'jiq', 'zuh', 'dyg', 'zha', 'hsn', 'zat', 'zhy', 'zyi', 'cgo']
            login()
            start=time.time()
            threads=[]
            for i in airCompanys:
                    threads.append(threading.Thread(target=t_getData,args=(i,airports)))
            for i in threads:
                  i.start()
            while reduce(lambda x,y:x or y,[ i.isalive() for i in threads]):
                    time.sleep(100)
                    print time.time()
            end=time.time()
            print end,start,end-start
    
    
    
  6. #3 楼 · 全冠清 · 2011-03-10 17:34
    
    #!/usr/bin/python
     
    #coding=utf-8
     
    import os,os.path,re,httplib,urlparse
     
     
    def validateUrl(url):
            up=urlparse.urlparse(url)
            http=httplib.HTTP(up.netloc)
            http.putrequest('GET',up.path)
            http.putheader('Accept','*/*')
            http.endheaders()
            errcode,errmsg,headers=http.getreply()
            if errcode==200:
                return False
            else:
                return True
     
    def main(path,types):
        l=[]
        for root,dirs,files in os.walk(path):
            for file in files:
                for i in types:
                    if file.endswith(i):
                       page=open(os.path.join(root,file)).read()
                       if re.search(r'http://image30\.19360\.cn/', page,re.M):
                           l+= [i[0] for  i in  re.findall(r'(http://image30\.19360\.cn/(\w|\.|_|/)+(\.gif|\.png|\.jpg|\.css))', page,re.M)]
        return filter(validateUrl,l)
     
     
    if __name__=='__main__':
        types=['.jsp','.html','.htm','.css']
        path="D:\\workspace\\19e3.0"
        l= main(path,types) 
        file=open('temp.txt','w')
        for i in l:
            print i.replace(r'http://image30.19360.cn','')
        file.writelines([i.replace(r'http://image30.19360.cn','')+'\n' for i in l])   
       
     
    
    
  7. #2 楼 · 全冠清 · 2011-02-12 17:20

    图片地址切换

    
    #!/usr/bin/python
     
    #coding=utf-8
     
    import os,os.path,re
     
     
    def main(path,rules):
        types=[key for key in rules]
        for root,dirs,files in os.walk(path):
            for file in files:
                for i in rules:
                    if file.endswith(i):
                       page=open(os.path.join(root,file)).read()
                       if re.search(r'\.jpg|\.gif|\.png', page):
                          for i in rules[i]:
                              if len(i)==3:
                                  page=re.sub(i[0], i[1],page)
                              else:
                                  page=page.replace(*i)
                          open(os.path.join(root,file),'w').write(page)
                          print os.path.join(root,file)
     
     
    if __name__=='__main__':
        rules={".jsp":[(r'http://image21.test.cn:18060', r'http://image30.19360.cn'),
                       (r'src="/(?=(\w|\.|_|/)+(gif"|png"|jpg"))',r'src="http://image30.19360.cn/',True),
                       (r'url(/',r'url(http://image30.19360.cn/'),
                       (r"src='/(?=(\w|\.|_|/)+(gif'|png'|jpg'))",r"src='http://image30.19360.cn/",True)],
           ".css":[(r'http://image21.test.cn:18060', r'http://image30.19360.cn'),
                   (r'url(/',r'url(http://image30.19360.cn/')],
           ".js":[(r'http://image21.test.cn:18060', r'http://image30.19360.cn')]}
        path="D:\\workspace\\testRefactoring\\WebRoot"
        main(path,rules) 
    
    
  8. #1 楼 · 全冠清 · 2011-02-12 16:14

    python删除svn文件

    
    for root,dirs,file in os.walk("D:\\workspace\\test"):
        if(root[-4:]==".svn"):
            print root
            os.system('attrib -r '+root+"\\*.* /s")
            shutil.rmtree(root)
    
    
    

评论功能维护中,当前仅展示历史评论。