Created
August 11, 2018 06:11
-
-
Save prespondek/1687e09bbdea84db57f6b645e1521afb to your computer and use it in GitHub Desktop.
Generates a list of files relative to a directory. Useful for make files.
This file contains hidden or 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
import sys, argparse, os, textwrap | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, | |
description=textwrap.dedent('''Relative Path Generator | |
----------------------- | |
Generates a list of files relative to a rootdirectory '''), | |
epilog='''Written by Peter Respondek''') | |
parser.add_argument("-r", help="directories will be relative to this position") | |
parser.add_argument("-x", help="only use this file extension") | |
parser.add_argument("root", help="root directory") | |
argv = parser.parse_args() | |
if not os.path.exists(argv.root) or (not argv.r and os.path.exists(argv.r)): | |
print ('path not valid') | |
sys.exit(2) | |
if not argv.r: | |
argv.r = argv.root | |
#print os.path.commonprefix([argv.root, argv.r]) | |
for (dirpath, dirnames, filenames) in os.walk(argv.root): | |
for file in filenames: | |
if argv.x: | |
name,ext = os.path.splitext(file) | |
if not ext == argv.x: | |
continue | |
print (os.path.relpath(os.path.join(dirpath,file), argv.r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment