Skip to content

Instantly share code, notes, and snippets.

@3panda
Created June 18, 2018 09:57
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 3panda/7c283e059ca9a03ead33020635d4ff8c to your computer and use it in GitHub Desktop.
Save 3panda/7c283e059ca9a03ead33020635d4ff8c to your computer and use it in GitHub Desktop.
TreeコマンドのPython版の試作
#!/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