备份文件的小程序

时间:2022-01-14 15:24:56
import os
import time


source=[r'F:\py123']


target_dir=r'F:\\'


today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')


if not os.path.exists(today):
    os.mkdir(today)
    print'Sucessfully created directory',today


target=today+os.sep+now+'.rar'


winrar_command="Winrar a {0} {1}".format(target,source)


if os.system(winrar_command)==0:
    print'Sucessfully backup to',target
else:

    print'Backup FAILED'

建立以年月日为目录的文件夹,压缩文件命名为当前时间。


import os
import time


source=[r'F:\py123']


target_dir=r'F:\\'


today=target_dir+time.strftime('%Y%m%d')
now=time.strftime('%H%m%S')


comment=raw_input('Enter a comment->')
if len(comment)==0:
    target=today+os.sep+now+'.rar'
else:
    target=today+os.sep+now+'_'+comment.replace('','_')+'.rar'




if not os.path.exists(today):
       os.mkdir(today)
       print'Sucessfully created directory',today


winrar_command="Winrar a {0} {1}".format(target,source)


if os.system(winrar_command)==0:
    print'Sucessful backup to',target
else:
    print'Backup FAILED'

在上一个程序的基础上可以人为添加待压缩文件名