Home > database >  'unexpected indent' in python
'unexpected indent' in python

Time:12-06

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

CodePudding user response:

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.

CodePudding user response:

it should be either this root = "C:\Voorleessoftware\" or this root = "C:\\Voorleessoftware\\"

always use \\ to represent path in windows

thank you

  • Related