Python 中的"意外缩进"



我正在用python编写一个程序,其中我读取一个文件路径,将该路径的不同部分放入变量中,更改路径并使用该路径创建新目录。一切似乎都很顺利。但是最后,当我尝试创建新目录时,我在

上得到"意外缩进"错误。
newFilePath.mkdir(parents=True)

我似乎找不到问题。

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:Voorleessoftware1 sept10u301e graad1A1test.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

之间没有缩进
newFilePath = rootDir + standaard + dag + graad + klas + uur + bestand

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

这意味着您的两个for块被解释为在前一个块之后结束,这反过来意味着您对最后一行的缩进是意外的。要查看差异,请尝试(用光标)在#nieuwe filepath之前标记空行,然后在# Dirs aanmaken nieuw filepath之前也标记空行-您将看到差异。

应该是这个root = "C:Voorleessoftware"或此root = "C:\Voorleessoftware\"

总是使用\来表示windows

中的路径谢谢

最新更新