私はあるフォルダから別のフォルダに基本的なバックアップスクリプトを書き込もうとしていますが、それを動作させましたが、ディレクトリ構造はコピーされませんでした。例えば、c:\temp\docs\file.txt
はd:\temp\docs\file.txt
だけではなくd:\temp\file.txt
pythonスクリプトがif文の字下げブロックを予想しました
私の問題は私の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)
は私が取得していますエラーです:
File "backup.py", line 15
new_target_folder = target_folder
^
IndentationError: expected an indented block
タブとスペースをミックスしていますか? – SuperSaiyan
リンターを使いたいかもしれません。コードを読みやすくし、PEPに準拠させるようにしてください。私はあなたのコードに問題がないと思うので、タブとスペースがインデントとして散在していると問題になると思います。 –