-2

I have zero coding/scripting skills, asked a friend online and looks like he's stuck as well.

I have many photos of peoples, which are named by a specific number. I also have an excel file which contains all matching numbers next to their names in 2 column. The script simply create a folder with the name and put the correct numbered files in them.

but I get this error: IndentationError: expected an indented block

here's the script

#!/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)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Can you please paste the error. BDW code is not properly indented. Python seperate's out block's of execution using indent. – satyam soni Oct 17 '19 at 12:16
  • 2
    Is the code you posted your actual code, with no indentation at all? If that's the case please read https://www.programiz.com/python-programming/statement-indentation-comments, in python indentation is syntactic, it's not optional. In your code, how do you expect it to understand where your loops end, your functions your conditions etc? – Dan Oct 17 '19 at 12:17
  • 2
    Possible duplicate of https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it – Iguananaut Oct 17 '19 at 12:18
  • converted the XLS file to UTF8 CSV, the script almost works, it creates folders but only half named and dosent moves any photos in them. For exemple, in the CSV, A1 have the number 6, and B1 the name Sarah, John. The script will create a folder named John" and wont move the file named 6 into the John" folder, wich should be names Sarah, John – Patric Nadeau Oct 17 '19 at 15:36

2 Answers2

1

This should be the properly indented code. Try running this and if it errors out, please check on which line it is failing

#!/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)
satyam soni
  • 259
  • 1
  • 9
0

Python has no char indicators {} for when a block of code starts of ends. It only knows a code belongs to the function if you indent it (usually with 4 spaces). For instance, where is the end of the for loop you wrote? In fact any code that should run as a block (loops, function definition, ifs) needs indentation.

for item in lista:
    do_something(item) # this will run inside the loop
i_am_out_of_the_loop = 5

Or:

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

I suggest you read a bit on the topic (or here).

missurunha
  • 108
  • 6