Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active June 12, 2020 03:34
Show Gist options
  • Save black-black-cat/005ea788b181f799ea6d0293ec6a82ef to your computer and use it in GitHub Desktop.
Save black-black-cat/005ea788b181f799ea6d0293ec6a82ef to your computer and use it in GitHub Desktop.
get relative path
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys, getopt
def get_relpath (target, start):
t = os.path.dirname(target)
s = os.path.dirname(start)
b = os.path.basename(target)
return os.path.join(os.path.relpath(t, s), b).replace('\\', '/')
def main():
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv,"ht:s:",["target=","start="])
except getopt.GetoptError:
print('test.py -t <target> -s <start>')
sys.exit(2)
for opt, arg in opts:
if opt == '-t':
target = arg
if opt == '-s':
start = arg
print(get_relpath(target, start))
if __name__ == "__main__":
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment