我正在尝试编写一个从一个文件夹到另一个文件夹的基本备份脚本,并且我让它工作 - 但目录结构没有被复制,只是文件。我也尝试复制子文件夹,例如,c:\temp\docs\file.txt转到d:\temp\docs\file.txt而不只是d:\temp\file.txt
我的问题存在于 if/else 语句的缩进中,但对我来说一切看起来都很好。我究竟做错了什么?
import datetime, time, string, os, shutil
COPY_FROM_LOCATION = 'C:\\xampp\\htdocs\\projects'
folder_date = time.strftime("%Y-%m-%d")
BACKUP_TO_LOCATION = 'D:\\BACKUP\\' + folder_date
#Create a new directory in D:\BACKUP based on today's date so the folder you're trying to copy to actually exists:
if not os.path.exists(BACKUP_TO_LOCATION):
os.makedirs(BACKUP_TO_LOCATION)
#copy function
def backup(source_folder, target_folder):
for subdir, dirs, files in os.walk(source_folder):
if subdir == source_folder :
new_target_folder = target_folder
else:
folder_name = subdir.split("C:\\xampp\\htdocs\\projects\\",1)[-1]
new_target_folder = target_folder + "\\" + folder_name
for file in files:
print "backing up: " + folder_name
shutil.copy2(os.path.join(subdir, file), new_target_folder)
backup(COPY_FROM_LOCATION,BACKUP_TO_LOCATION)
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误:
File "backup.py", line 15
new_target_folder = target_folder
^
IndentationError: expected an indented block
Run Code Online (Sandbox Code Playgroud)