---
# System prepended metadata

title: 無標題

---

```python
import os, time

def walkThroughTree(dir_name:str, log_file, prefix:str=""):
    try:
        cur_dir_list = os.listdir(dir_name)
    except:
        print("this is not a directory")
        return
    
    if cur_dir_list == []:
        return
    
    dir_list = []
    file_list = []
    for file_or_dir in cur_dir_list:
        try:
            os.listdir(dir_name + os.sep + file_or_dir)
            dir_list.append(file_or_dir)
        except:
            file_list.append(file_or_dir)
    
    file_list = sorted(list(zip(file_list, [False] * len(file_list))))
    dir_list = sorted(list(zip(dir_list, [True] * len(dir_list))))
    
    total_list = file_list + dir_list # print file first :)
    
    
    for name, is_dir in total_list[:-1]:
        print(prefix + "├── " + name)
        log_file.write(prefix + "├── " + name + "\n")
        if is_dir:
            walkThroughTree(dir_name + os.sep + name, log_file, prefix + "|   ")
    
    last_name, last_is_dir = total_list[-1]
    print(prefix + "└── " + last_name)
    log_file.write(prefix + "└── " + last_name + "\n")
    if last_is_dir:
        walkThroughTree(dir_name + os.sep + last_name, log_file, prefix + "    ") 
        
if __name__ == "__main__":
    target_path = input("please enter the path of target directory\n")
    cur_time = time.strftime("%Y%m%H%M%S", time.localtime(time.time()))
    log_file = open(f"log_{cur_time}.txt", 'a')
    walkThroughTree(target_path, log_file)
    log_file.close()
    
```