Created
December 9, 2021 22:56
-
-
Save remram44/aa7ecb61dba33bc926f579dc25bf2c77 to your computer and use it in GitHub Desktop.
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
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