Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import sys
- def uri_to_path(uri):
- '''Convert a file URI to a path'''
- try:
- # gio is easier to use
- import gio
- path = gio.File(uri).get_path()
- except ImportError:
- # if gio isn't available
- import urllib
- # get the path to file
- path = ""
- if uri.startswith('file:\\\\\\'): # windows
- path = uri[8:] # 8 is len('file:///')
- elif uri.startswith('file://'): # nautilus, rox
- path = uri[7:] # 7 is len('file://')
- elif uri.startswith('file:'): # xffm
- path = uri[5:] # 5 is len('file:')
- path = urllib.url2pathname(path) # escape special chars
- path = path.strip('\r\n\x00') # remove \r\n and NULL
- return path
- def run():
- if len(sys.argv) <= 1:
- print 'no arguments passed'
- sys.exit(1)
- else:
- # for uri in sys.argv[1:]:
- for uri in sys.argv[1:]:
- path = uri_to_path(uri)
- #print uri + ": " + path
- print path
- sys.exit(0)
- if __name__ == '__main__':
- run()
- # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement