【问题标题】:shutil.copy fails after os.makedirsshutil.copy 在 os.makedirs 之后失败
【发布时间】:2020-06-07 03:40:14
【问题描述】:

如您所见,我正在尝试为自己创建一个小型备份脚本,以选择所需的文件并进行备份。

import shutil
import datetime
import os
import time
def backup():
    # set the update interval
    while True:
        backup_interval = input("Please enter the backup interval in seconds: ") # 300
        try:
            valid_time = int(backup_interval) // 60
            print("Backup time set to:", valid_time, "minutes!")
            break
        except ValueError:
            print("This time is not valid, please enter a correct time in seconds: ")
            print(">>> 60 seconds = 1 minute, 3600 seconds = 60 minutes.")

    backup_file = input(r"Please enter the path for the file to backup: ") # D:\Python\BackupDB\test.db"
    dest_dir = input(r"Please enter the destination path: ") # D:\Python\BackupDB\
    folder_name = input(r"Please name your backup folder: ") # BD_Backup
    now = str(datetime.datetime.now())[:19]
    now = now.replace(":", "_")
    # backup_file = backup_file.replace(backup_file, backup_file + str(now) + ".db")
    # thats why I got the FileNotFoundError
    final_destination = os.path.join(dest_dir, folder_name)
    if not os.path.exists(final_destination):
        os.makedirs(final_destination)

    print("hello world")
    shutil.copy(backup_file, final_destination)

第一个问题是,将文件复制到目标文件夹后如何替换名称以获得类似 test.db -> test_2020-02-23 08_36_22.db 喜欢这里:

source_dir = r"D:\Python\BackupDB\test.db"
destination_dir = r"D:\Python\BackupDB\BD_Backup\test_" + str(now) + ".db"
shutil.copy(source_dir, destination_dir)

输出:

test_2020-02-23 08_36_22.db

我在这里做错了什么? 以及如何将文件复制 5 次并在一段时间后 (backup_interval) 删除第一个并将最后 4 个向上移动并创建一个新文件,这样我总共拥有该文件的 5 个副本?

【问题讨论】:

    标签: python-3.x database error-handling backup shutil


    【解决方案1】:

    我之前遇到过类似的问题,原因是目录的创建在尝试访问之前尚未完成。在复制之前简单的睡眠应该可以确认这一点。

    【讨论】:

    • 好吧,我也对此表示怀疑,但 python 会逐行执行。所以问题就在这里:backup_file = backup_file.replace(backup_file, backup_file + str(now) + ".db") 我更改了我试图复制的文件:D
    【解决方案2】:

    我已根据您的需要修改了您的代码,

            backup_file = input(r"Please enter the path for the file to backup: ") # D:\Python\BackupDB\test.db"
            dest_dir = input(r"Please enter the destination path: ") # D:\Python\BackupDB\
            folder_name = input(r"Please name your backup folder: ") # BD_Backup
            old_file_name=backup_file.split("/")[-1]
            now = str(datetime.datetime.now())[:19]
            now = now.replace(":", "_")
            new_file_name = old_file_name.split(".")[0]+"_" + str(now) + ".db"
            final_destination = os.path.join(dest_dir, folder_name)
            if not os.path.exists(final_destination):
                os.mkdir(final_destination)
            new_file="/"+new_file_name
            shutil.copy(backup_file, final_destination)
            os.rename(final_destination+'/'+old_file_name,final_destination+new_file)
    

    我喜欢,复制文件后,我只是重命名它

    【讨论】:

      猜你喜欢
      • 2018-08-08
      • 2013-11-29
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 2015-11-13
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多