Let python delete the file under window

Source: Internet
Author: User
Tags python script

Today received a call, there is a downstairs customer response, the data has a problem, I connected to the server, found broke a disk character space is full, resulting in, application in error, stop service, delete a huge log file, service opened, all normal.

Long wanted to write a script on the server, today is just empty, so ba la a bit, wrote a small Python script, Daniel does not have to read, the rationale is to find out the modification of each file time, if the modification time is not the same day, if the change is less than today, then we delete this file . Write a coarser, look at the code:

#!/usr/bin/env pythonimport osimport shutilimport timeimport datetimepath1= ' c:\\backup_queue_broker_shtumor_ 20130218\\log\\ ' #path2 = ' c:\\backup_other_broker_shtumor_20130218\log\\ ' #path3 = ' c:\\backup_schedule_broker_ Shtumor_20130807\\log\\ ' def deletefile (path): Today=datetime.datetime.today () file=os.listdir (path) for I in file: Mtime=os.stat (path+i). St_mtime #得到文件的修改时间标记format = '%y-%m-%d ' value=time.localtime (mtime) dt=time.strftime (format, Value) Dt1=datetime.datetime.strptime (dt,format) #转换下时间格式today = (str (today)) [: 10]dt1=str (DT1) [: ten] #print dt1#print Todayif Dt1 < today:p rint "Begin to Delete File", Path+ios.remove (path+i) else:passdeletefile (path1) #deleteFile ( path2) #deleteFile (PATH3)

Hehe, finally bottled into a function, should have multiple paths of files need to be deleted, if there is a better way, please do not enlighten us ...

This article is from "You are a passer-by or fearless" blog, please be sure to keep this source http://world77.blog.51cto.com/414605/1576599

Let python delete the file under window

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.