Skip to content

Instantly share code, notes, and snippets.

@remram44
Created December 9, 2021 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remram44/aa7ecb61dba33bc926f579dc25bf2c77 to your computer and use it in GitHub Desktop.
Save remram44/aa7ecb61dba33bc926f579dc25bf2c77 to your computer and use it in GitHub Desktop.
def listdir_relative(path):
result = []
cwd = os.getcwd() + '/'
for entry in listdir(path):
if not entry.startswith(cwd):
fail('Unexpected entry ' + entry)
entry = entry[len(cwd):]
result.append(entry)
return result
def listdir_relative_with_dirs(path):
result = {}
root = os.path.join(os.getcwd(), path) + '/'
for entry in listdir(path, recursive=True):
# Make the entry relative
if not entry.startswith(root):
fail('Unexpected entry ' + entry)
entry = entry[len(root):]
# Is it in a subdirectory?
slash = entry.find('/')
if slash == -1:
# Top-level, add it
result[entry] = True
else:
# Subdirectory, add the subdirectory itself
result[entry[:slash]] = True
if path == '.':
return result.keys()
else:
return [os.path.join(path, entry) for entry in result.keys()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment