Python實現備份檔案例項

2022-10-06 05:24:13 字數 3340 閱讀 9459

該例項主要實現讀取乙個任務檔案, 根據指定的任務引數自動備份.

任務檔案的格式: (注意,分號後面注釋是不支援的)

[task] ; 一項任務開始

dir=h:/project ; 指定備份的目錄

recusive=1 ; 是否遞迴子目錄

suffix=h|cpp|hpp|c|user|filters|vcxproj|sln|css|gif|html|bmp|png|lib|dsw|dsp|htm|html|ico|ini|jpg|rc|vssscc ; 指定備份的副檔名

exclude=0 ; 指定是備份上面的引數指定的副檔名還是排除指定的副檔名

; 備份後的檔案路徑名

python**如下:

# -*- coding: utf-8 -*-

import sys

import os

import zipfile

class task:

#dir str directory

#bsub bool include subdirectory

#sfx str postsuffix ,sepeated by '|'

#ecld bool include or execlude the postsuffix sfx

def __init__(self,dir,bsub,sfx,ecld,zip):

self.dir = dir

self.bsub = bsub

self.suffix = sfx.split("|")

self.exclude = ecld

self.zip = zip

@staticmethod

def isfilter(sfx,sfxs,bexcld):

bfound = false

for e in sfxs:

if e == sfx:

bfound = true

break

if bexcld:

return not bfound;

else:

return bfound;

class qbackup:

'''備份指定目錄下具備指定副檔名的檔案'''

def __init__(self):

self._list =

def __del__(self):

pass

#tfile 任務檔案

def readtask(self,tfile):

dir = ""

bsub = false

sfx = ""

becld = false

zip = ""

try:

f = open(tfile,'r')

while true:

line = f.readline()

if len(line) == 0:

break;

line = line.strip(" ")

if "[task]/n".lower() == line.lower():

# 讀取接下來的4行

iline = 1

while iline <= 5:

line = f.readline()

line = line.strip(" /t/n") # 去除前後的空白符

idx = line.find("=")

if -1 == idx:

break;

atti = line[0:idx]

value = line[idx+1:]

print(value)

if "dir" == atti:

dir = value

elif "recusive" == atti:

bsub = bool(int(value))

elif "suffix" == atti:

sufix = value

elif "exclude" == atti:

becld = bool(int(value))

elif "zip" == atti:

zip = value

else:

break

iline += 1

else:

t = task(dir,bsub,sufix,becld,zip)

self._list.append(t)

except:

return false

return true

def dobackup(self):

for e in self._list:

try:

zip = zipfile.zipfile(e.zip,'w',zipfile.zip_deflated)

self.zipdir(zip,e.dir,e.bsub,e.suffix,e.exc

zip.close()

except:

print("exception raised!")

return false

return true

def zipdir(self,zip,dir,bsub,sfxs,ecld):

subdir = ""

path = ""

if os.path.isdir(dir):

paths = os.listdir(dir)

#備份本目錄

print("zipdir: ",dir)

for e in paths:

程式設計客棧 path = dir + "/" + e

ext = os.path.splitext(e)[1][1:]

if os.path.isfile(path) and task.isfilter(ext,sfxs,ecld):

print ("zipfile: ",path)

zip.write(path)

#清理子目錄

if bsub:

for e in paths:

subdir = dir + "/" + e程式設計客棧

self.zipdir(zip,subdir,bsub,sfxs,ecld)

def printtask(self):

for e in self._list:

print (e.dir,e.bsub,e.suffix,e.exclude,e.zip)

if '__main__' == __name__:

c = qbackup()

c程式設計客棧.readtask("bkup.txt")

c.dobackup()

本文標題: python實現備份檔案例項

本文位址:

python 備份檔案指令碼

usr bin env python filename backup ver1.pyimport os import time source r d python test r d python test1 target dir d python test2 remember to change t...

Python 備份檔案,以及備份大檔案

今天分享乙個很有用的小 就是關於檔案的備份 import os 匯入os模組 ori file name r e python mayday.輸入檔案路徑 if os.path.isfile ori file name 判斷該路徑的是否是檔案 擷取檔名,重組檔名 seek num ori file ...

linux指令碼實現備份檔案

要求 編寫乙個指令碼實現備份 var log目錄下的所有檔案到 bak目錄下,要求檔名是包含當天日期,精確到秒,檔名例如 2019 6 6 2 30 20 log.tar.gz。同時要求刪除 bak目錄下七天前的備份檔案,只保留最近7天的 bin bash 獲得當前的時間 ctime date y ...