Last active
June 12, 2020 03:34
-
-
Save black-black-cat/005ea788b181f799ea6d0293ec6a82ef to your computer and use it in GitHub Desktop.
get relative path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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