Reputation: 153
I have following codeline written in python 2.7. I am using centos6 as OS. Right now I am new to python & trying to learn it.
Following code line copies all specific files (i.e. containing letter "R") in the folder /2013_06_02 from remote machine 192.168.13.152 to my local machine (tony is my username on my local machine; while john is my username on remote machine)
os.system("scp [email protected]:/home/john/2013_06_02/11_RBPLJ1635+3808*R* /home/tony/data/")
This works. (This code line asks password). And I get specific files in my /data directory on my local machine.
Now the problem part: Actually I have many folders on remote machine, each one contains many files with filenames which contain letter "R". Also Each filename begins with some no. like 11 in above example. Now I want to vary folder name and at the same time vary no. appearing in front of filename.
I tried this:
date = "2013_10_05"
mynum = "11"
os.system("scp [email protected]:/home/john/date/mynum_RBPLJ1635+3808*R* /home/tony/data/")
But I got this error:
SyntaxError: EOL while scanning string literal
I tried this way also:
date = "2013_10_05"
mynum = "11"
os.system("scp [email protected]:/home/john/"date"/"mynum"_RBPLJ1635+3808*R* /home/tony/data/")
But this did not work.
In short I have to make folder name & file no. to be two variables to be used in scp command so that code will automate copying process; i.e. I don't have to change folder name & no. in filename by hand everytime.
I know that every time I have to type password to remote machine, if above code line works. But I don't care for that at this moment. First I want to get above codeline into working condition. Then I want to see how I can do above stuff without typing password everytime.
Any help will be highly appreciated. Thanks in advance.
Upvotes: 0
Views: 1377
Reputation: 180482
Use str.format to pass in variables:
date = "2013_10_05"
mynum = "11"
os.system("scp [email protected]:/home/john/{}/{}_RBPLJ1635+3808*R* /home/tony/data/".format(date,mynum))
Using:
os.system("scp [email protected]:/home/john/"date"/"mynum"_RBPLJ1635+3808*R* /home/tony/data/")
you have double quotes inside double quotes which is neither valid syntax in python or passing in the variables.
Upvotes: 2
Reputation: 3491
One more way to run is:
date = "2013_10_05"
mynum = "11"
os.system("scp [email protected]:/home/john/%s/%s_RBPLJ1635+3808*R* /home/tony/data/" %(date, mynum))
Upvotes: 0
Reputation: 87134
Use string formatting.
date = "2013_10_05"
mynum = "11"
os.system("scp [email protected]:/home/john/{}/{}_RBPLJ1635+3808*R* /home/tony/data/".format(date, mynum))
I recommend that you take a look at fabric. It's great for this sort of thing.
Upvotes: 1