Created
June 18, 2018 09:57
-
-
Save 3panda/7c283e059ca9a03ead33020635d4ff8c to your computer and use it in GitHub Desktop.
TreeコマンドのPython版の試作
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/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import sys | |
import glob | |
def main(path, layer=0, is_root=False): | |
tab = u" " | |
if is_root is True: | |
file_path = glob.glob(path) | |
root_name = ','.join(file_path) | |
print(root_name) | |
files = glob.glob(path + '/*') | |
for file in files: | |
# get dir or file path | |
file_paths = file.split('/') | |
# get dir or file name | |
print(tab * (layer + 1) + "|-" + file_paths.pop()) | |
if os.path.isdir(file): | |
# case dir (recall) | |
main(file, layer + 1) | |
if __name__ == '__main__': | |
if len(sys.argv) == 2: | |
main(path=sys.argv[1], is_root=True) | |
else: | |
print("usage.") | |
print("{filename} <path(ex:/root)>>" | |
.format(filename=sys.argv[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment