用于将文件移动到文件夹中的Python脚本,IndentationError
“我没有任何编码/脚本技能,”一位在线朋友问道,看起来他也被卡住了 我有很多人的照片,都是用特定的数字命名的。我也有一个excel文件,其中包含所有匹配的数字旁边的名称在2列。该脚本只需创建一个名为的文件夹,并将正确编号的文件放入其中 但是我得到了这个错误:IndentationError:应该是一个缩进块 这是剧本用于将文件移动到文件夹中的Python脚本,IndentationError,python,Python,“我没有任何编码/脚本技能,”一位在线朋友问道,看起来他也被卡住了 我有很多人的照片,都是用特定的数字命名的。我也有一个excel文件,其中包含所有匹配的数字旁边的名称在2列。该脚本只需创建一个名为的文件夹,并将正确编号的文件放入其中 但是我得到了这个错误:IndentationError:应该是一个缩进块 这是剧本 #!/usr/local/bin/python3 import os, shutil, pathlib, fnmatch, sys def move_dir(src: str,
#!/usr/local/bin/python3
import os, shutil, pathlib, fnmatch, sys
def move_dir(src: str, dst: str, prefix:str, suffix: str):
if not os.path.isdir(dst):
pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
for f in fnmatch.filter(os.listdir(src), prefix + suffix):
shutil.move(os.path.join(src, f), os.path.join(dst, f))
def readMapping(src: str):
mappings = {}
with open(src) as inputFile:
for line in inputFile:
args = line.rstrip().split(None, 1)
mappings[args[0]] = args[1]
return mappings
def moveFilesMatchingMapping(mappings: dict, source: str, types:str):
for num, moveTo in mappings.items():
move_dir(src=source, dst=os.path.join(source, moveTo), prefix=num, suffix=types)
sourceDir = sys.argv[2]
fileType = "*" + os.getenv("HKS_TYPE", "jpg")
mappingFile = sys.argv[1]
moveFilesMatchingMapping(mappings=readMapping(mappingFile),source=sourceDir, types=fileType)
这应该是正确缩进的代码。尝试运行此程序,如果出错,请检查失败的行
#!/usr/local/bin/python3
import os, shutil, pathlib, fnmatch, sys
def move_dir(src: str, dst: str, prefix:str, suffix: str):
if not os.path.isdir(dst):
pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
for f in fnmatch.filter(os.listdir(src), prefix + suffix):
shutil.move(os.path.join(src, f), os.path.join(dst, f))
def readMapping(src: str):
mappings = {}
with open(src) as inputFile:
for line in inputFile:
args = line.rstrip().split(None, 1)
mappings[args[0]] = args[1]
return mappings
def moveFilesMatchingMapping(mappings: dict, source: str, types:str):
for num, moveTo in mappings.items():
move_dir(src=source, dst=os.path.join(source, moveTo), prefix=num, suffix=types)
sourceDir = sys.argv[2]
fileType = "*" + os.getenv("HKS_TYPE", "jpg")
mappingFile = sys.argv[1]
moveFilesMatchingMapping(mappings=readMapping(mappingFile), source=sourceDir,
types=fileType)
Python没有字符指示符{},用于指示代码块何时开始或何时结束。如果缩进一个代码(通常有4个空格),它才知道该代码属于该函数。例如,您编写的For循环的结尾在哪里?事实上,任何应该作为块运行的代码(循环、函数定义、ifs)都需要缩进
for item in lista:
do_something(item) # this will run inside the loop
i_am_out_of_the_loop = 5
或:
我建议你在(或)上读一读 你能粘贴错误吗。BDW代码未正确缩进。PythonSeparate使用缩进执行的代码块。您发布的代码是您的实际代码,没有缩进吗?如果是这种情况,请阅读,在python中,缩进是语法的,不是可选的。在您的代码中,您希望它如何理解循环的结束位置、函数和条件等?XLS文件转换为UTF8 CSV的可能重复,脚本几乎可以工作,它创建文件夹,但只命名了一半,dosent会移动其中的任何照片。例如,在CSV中,A1的数字为6,B1的名称为Sarah,John。脚本将创建一个名为“John”的文件夹,并且不会将名为6的文件移动到“John”文件夹中,该文件夹的名称应为Sarah,John
if x > 10:
y = 10 # this only runs if x is greater than 10
z = 5 # this only runs if x is greater than 10
z = 20 # this runs regardless of the value of x