Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

James Joseph
James Joseph
4,885 Points

Python: import subprocess [SOLVED]

Hey guys, I've just finished the first two parts of beginner to python and I thought I'd challenge myself by doing a simple script before starting to make the first app tutorial. I'm doing an rsync script and I'm trying to run the rsync command.

I've looked into it and saw about the subprocess.call command but I just want to confirm if this is correct?

elif startbk == 1:
    print("Starting backup, rsync -avv {} {} --log-file={}".format(sor,des,logloc))
    import subprocess
    subprocess.call(['rsync', '-avv', sor des '--log-file=' logloc])

Any assistance or input on this is greatly appreciated.

James Joseph
James Joseph
4,885 Points

I looked into it a bit further and I figured out how to do it, I've redesigned my script but my issue with doing the subprocess call was that it doesn't seem to take variables like that in a call. You have to make the variable first with the commands and then call the variable.

My solution:

import os
from subprocess import call

            elif logch == 'n':
                print("We will now start the backup!")
                print("Starting backup, rsync -avv {} {} --log-file={}".format(sor,des,logloc))
                lclsyn = ["rsync", "-avv", sor, des, "--log-file", logloc]
                call(lclsyn)
                time.sleep(1)
                print("The backup is now complete! Check the logs at {} for details on what was backed up".format(logloc))