Skip to content

Instantly share code, notes, and snippets.

@erning
Forked from sp3c73r2038/path.py
Created December 17, 2012 08:26
Show Gist options
  • Save erning/4316644 to your computer and use it in GitHub Desktop.
Save erning/4316644 to your computer and use it in GitHub Desktop.
golang
/*
put this script in the path like
assumed /tmp (cwd)
/tmp/dir1/dir2/path.go
/tmp/dir3 symlinked to /tmp/dir1/dir2
and run with
gorun ../tmp/dir3/path.go
or build path.go and run with ../tmp/dir3/path
*/
package main
import "os"
import "fmt"
import "path/filepath"
func main() {
p := os.Args[0]
var s string
fmt.Printf("original: %s\n", p)
s = filepath.Clean(p)
fmt.Printf("normpath: %v\n", s)
s, _ = filepath.Abs(p)
fmt.Printf("abspath: %v\n", s)
s, _ = filepath.EvalSymlinks(p)
s, _ = filepath.Abs(s)
fmt.Printf("realpath: %v\n", s)
s, _ = os.Getwd()
s = filepath.Join(s, p)
fmt.Printf("normpath(join(getcwd(), p): %v\n", s)
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# put this script in the path like
# assumed /tmp (cwd)
# /tmp/dir1/dir2/path.py
# /tmp/dir3 symlinked to /tmp/dir1/dir2
#
# and run with
# python ../tmp/hello3/path.py
from os import getcwd
from os.path import normpath, abspath, realpath, join
import sys
p = sys.argv[0]
print "origin: %s" % p
print "normpath: %s" % normpath(p)
print "abspath: %s" % abspath(p)
print "realpath: %s" % realpath(p)
print "normpath(join(getcwd(), p): %s" % normpath(join(getcwd(), p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment