Python进阶开发-Windows系统备份小程序v1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39591494/article/details/82824464

Python-Windows系统备份小程序v1


#!/usr/bin/env python
# -*- coding:utf-8 -*-

import zipfile
import os
import sys
import time

__author__ = "YanZanG"

class Back_file(object):
    """程序备份类"""
    def __init__(self,file_name, time):
        self.file_name = file_name
        self.time = time

    def zip_file(self):
        print(os.getcwd())
        My_zip = zipfile.ZipFile(f"{self.time}_back.zip", "w")
        os.chdir(self.file_name)
        Count = True
        while (Count):
            for root, dirs, files in os.walk(self.file_name):
                for name in files:
                    file_result = os.path.join(root, name)
                    print(file_result)
                    My_zip.write(file_result, compress_type=zipfile.ZIP_DEFLATED)
                    Count = False
            My_zip.close()
            print(f"恭喜您,备份成功,备份文件为:{self.time}_back.zip")

def main():
    Count = True

    while (Count):
        print(f"Welcome to {__author__} backup system.")

        Your_file_input = input("请您输入您需要备份的目录:".strip())

        if os.path.exists(Your_file_input):
            os.chdir(Your_file_input)
            print("目录检测成功,正在备份文件,请稍后.....")
            Time = time.strftime("%Y_%m_%d%H%M%S", time.localtime())

            back_file = Back_file(Your_file_input, Time)
            back_file.zip_file()
        else:
            print("备份目录失败,您输入的内容检测失败,请您检查后再次输入,")


if __name__ == '__main__':

    while True:
        menu = {
            "1": "进入备份程序",
            "q": "退出此程序",
        }

        for k, v in menu.items():
            print(f"{k}、{v}")
        Your_input = input("请您选择:".strip()).upper()
        if Your_input == "1":
            main()


        elif Your_input == "Q":
            print("欢迎您再次使用,再见~")
            sys.exit()
        else:
            print("请您选择(1/q)")

猜你喜欢

转载自blog.csdn.net/qq_39591494/article/details/82824464
v1