Skip to content

Instantly share code, notes, and snippets.

@jonlabelle
Last active December 25, 2015 05:49
Show Gist options
  • Save jonlabelle/6927275 to your computer and use it in GitHub Desktop.
Save jonlabelle/6927275 to your computer and use it in GitHub Desktop.
Return the canonical path (real path) of the specified filename, eliminating any symbolic links encountered in the path.
#!/usr/bin/env python
import os.path as filepath
import sys
from optparse import OptionParser
def main():
parser = OptionParser(
prog='rp', usage="%prog [path]\n\nReturn the canonical path of the specified filename\n\n [path]\tpath of file to check\n\n when *no args* are passed, the current working directory\n will be set as [path].")
options, args = parser.parse_args()
paths = []
if len(sys.argv) == 2 and sys.argv[1] == '-':
for line in sys.stdin:
paths.append(line.strip())
else:
for fp in sys.argv[1:]:
paths.append(fp)
if len(paths) > 0:
for p in paths:
if filepath.exists(p):
print filepath.realpath(p)
else:
print p + ': No such file or directory.'
else:
print filepath.realpath('')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment