Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

'unexpected indent' in python

I am making a program in python where I read a filepath, put different pieces of that path in variables, change the path and make a new dir with that path. Everything almost seems to work. But at the end when I try to create the new dirs I am getting ‘unexpected indent’ error on

newFilePath.mkdir(parents=True)

I can’t seem to find the problem.

import os
import shutil
from pathlib import Path


root = "C:\Voorleessoftware\\"
path = os.path.join(root, "targetdirectory")

for path, subdirs, files in os.walk(root):
    for name in files:
        filePath = str(os.path.join(path, name))
        #filepath voorbeeld = c:\Voorleessoftware\1 sept\10u30\1e graad\1A1\test.txt
        
        findchar1 = '\\'
        list = [pos for pos, char in enumerate(filePath) if char == findchar1]
        rootDir   = filePath[0:list[0]]
        standaard = filePath[list[0]:list[1]]
        dag       = filePath[list[1]:list[2]]
        uur       = filePath[list[2]:list[3]]
        graad     = filePath[list[3]:list[4]]
        klas      = filePath[list[4]:list[5]]
        bestand   = filePath[list[5]:len(filePath)-1]
        
        #nieuwe filepath 
        newFilePath = rootDir + standaard + dag + graad + klas + uur + bestand

        # Dirs aanmaken nieuw filepath
        newFilePath.mkdir(parents=True)

Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have no indentation between

newFilePath = rootDir + standaard + dag + graad + klas + uur + bestand

and

# Dirs aanmaken nieuw filepath
newFilePath.mkdir(parents=True)

Which means your two for blocks are interpreted as ending after the former, which in turn means that your indentations for the last line are unexpected. To see the difference, try marking (with your cursor) the empty line before the #nieuwe filepath, and then also the empty line before the # Dirs aanmaken nieuw filepath – you’ll see the difference.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading