Source

pythonwise / putpath

#!/usr/bin/env python
'''Put a path in clipboard'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

from os import popen
from os.path import abspath, exists

def putpath(path):
    # putclip is another utility here
    pipe = popen("putclip", "w")
    pipe.write(abspath(path))

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser

    # Command line parsing
    p = OptionParser("usage: %prog [PATH]")

    opts, args = p.parse_args(argv[1:])
    if len(args) not in (0, 1):
        p.error("wrong number of arguments") # Will exit

    if len(args) == 0:
        path = "."
    else:
        path = args[0]

    if not exists(path):
        raise SystemExit("error: %s - no such file or directory" % path)

    putpath(path)

if __name__ == "__main__":
    main()