Whenever I need to solve a practical computing problem that requires writing a script, tracking down the right tool for the job is often the trickiest part. Especially when I am learning a new language, it is easy to fall down the rabbit hole of documentation and extra libraries (not to mention threads on discussion forums).
Today, I wanted to write a script that would automatically upload a particular file on my local machine to my server that hosts my website. Basically, I got tired of constantly having to remember and type out the (long) destination path for every file I wanted to upload. Since I am learning Python, I thought I would figure out how to solve the problem in Python.
pip install git+git://github.com/tomerfiliba/plumbum.git
Now with Plumbum installed, I was able to put together this code which does exactly what I want so far:
#!/usr/bin/env python import plumbum import sys host = "willrosenbaum.com" username = "username" key = "/path/to/my/public/key" local_prefix = "/home/username1/Dropbox/" remote_prefix = "/home/username2/Dropbox/" filename = sys.argv r = plumbum.machines.SshMachine(host, user = username, keyfile = key) fro = plumbum.local.path(filename) remote_path = str(fro).replace(local_prefix, remote_prefix) to = r.path(remote_path) plumbum.path.utils.copy(fro, to)
Now whenever I want to copy a file from my local machine to its corresponding directory on my server, all I have to do is run
instead of the old way
scp filename email@example.com:/path/i/cant/seem/to/remember
I think it’s a small victory for efficiency, but a victory nonetheless.