我的编码/脚本技能为0,在网上问了一个朋友,看上去他也很固执。
我有很多人的照片,但都用一个特定的数字命名。我也有一个excel文件,该文件在2列中的名称旁边包含所有匹配的数字。该脚本只需创建一个具有名称的文件夹,然后将正确编号的文件放入其中。
但是我收到此错误:IndentationError:预期为缩进的块
这里是脚本
#!/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)