Python script with Robocopy subprocess - Exit Codes

40 views Asked by At

I've got this loop, which is iterate through a DF that has a column with robocopy commands, I want to run the command and then attribute another column with whether the copy was a success or not:

#cmd is below from earlier in script    
#'robocopy "{source_path}" "{target_path}" "file.json" /mt /r:0 /S'

for index, row in df_subset.iterrows():
    robocopy_command = row['Robocopy_Command']
    try:
        subprocess.run(robocopy_command, shell=True, check=True)
        df_subset.loc[index, 'Robocopy_Success'] = 'YES'
        print(f"Robocopy command for row {index} executed successfully.")
    except subprocess.CalledProcessError as e:
        df_subset.loc[index, 'Robocopy_Success'] = 'NO'
        print(f"Error executing Robocopy command for row {index}: {e}")

The script is printing the following message for all the rows:

Error executing Robocopy command for row 12: Command 'robocopy "source" "dest" "file.json" /mt /r:0 /S' returned non-zero exit status 1.

And thus the 'Robocopy_Success' column is being set to No

However according to Robocopy Exit Codes '1' is a successful run. I'm guessing it's the difference between subprocess and robocopy exit codes causing this but I can't seem to get it working in unison or find examples online.

How do I get the script to interpret the exit codes correctly?

0

There are 0 answers