1

I'm trying to write a basic backup script from one folder to another, and I got it to work - but the directory structure was not being copied over, just the files. I'm trying to copy in the subfolder as well, so that, for example, c:\temp\docs\file.txt goes to d:\temp\docs\file.txt instead of just d:\temp\file.txt

My issue exists in indentation with my if/else statement, but everything looks good to me. What am I doing wrong?

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)

Here's the error I'm getting:

  File "backup.py", line 15
    new_target_folder = target_folder
                    ^
IndentationError: expected an indented block
2
  • 2
    Are you mixing tabs and spaces ? Commented Jun 22, 2016 at 17:52
  • You might want to use a linter. Make your code easier to read and comply with PEP. I don't see an issue with your code, which makes me think it is a problem with interspersing tabs and spaces as indentation. Commented Jun 22, 2016 at 17:54

3 Answers 3

5

You're intermixing tabs and spaces.

enter image description here

Use one or the other, not both. Preferably spaces.

Sign up to request clarification or add additional context in comments.

1 Comment

yup. for the love of all that is holy, i was about to pull my hair out!
1

This error typically means there is an error in indentation. Check you don't mix tabs and spaces.

You can use https://www.pylint.org/ to detect them or if it something simple paste the code at http://pep8online.com, it will show you what you can enhance.

Comments

0

what's up with the weirdness with the space before the semi-colon? I've not seen it done that way before, that appears to be where this script is choking up.

change

if subdir == source_folder :

with

if subdir == source_folder:

2 Comments

No, it's fine to have a space before the colon. That's not the problem.
have you tried to fix that though, somehow your newline characters and or spaces are messed up causing this indentation error to occur. Did you copy and paste this script from somewhere?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.