Created
July 23, 2018 03:35
-
-
Save fragmuffin/b187264779afb0dfd2a6e5533d5e6de0 to your computer and use it in GitHub Desktop.
Python OS system call with subprocess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import subprocess | |
# Set sync source & destination | |
source_path = "temp/a" | |
dest_path = "temp/b" | |
# Sync: source_path -> mountpoint | |
if sys.platform.startswith('win'): | |
# Windows: Robocopy.exe | |
cmd = [ | |
'Robocopy.exe', | |
os.path.abspath(source_path), | |
os.path.abspath(dest_path), | |
'/MIR', '/Z', '/W:5', | |
] | |
else: | |
# Linux: rsync | |
cmd = [ | |
'rsync', | |
'-aIvzh', '--delete', | |
"{}/".format(os.path.abspath(source_path)), | |
"{}/".format(os.path.abspath(dest_path)), | |
] | |
# Create & Run process | |
process = subprocess.Popen( | |
cmd, shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
for line in process.stdout: | |
process.poll() | |
print(line.decode().rstrip('\n')) | |
process.wait() | |
print("Process finished with return code: {}".format(process.returncode)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment