Using python under Mac to implement a simple directory tree presentation method
- 2021-01-19 22:18:23
- OfStack
The tree command can be used to view the directory tree in either Linux or Windows, and the operation is similar in both operating systems. When using Linux, it was initially assumed that this command was available in shell. It turns out that this command is not available when using Mac.
In order to achieve a similar function, using python made a simple small script. Can not achieve a beautiful directory tree, but you can achieve the file and directory list printing.
Write the code as follows:
#!/usr/bin/python
import os
pwd = os.getcwd()
for root,dirs,files in os.walk(pwd):
os.chdir(root)
for f in files:
if not(f == 'file_list.py' and root == pwd):
print("%s<->%s" %(f,root))
os.chdir(pwd)
The program execution results are as follows:
python file_list.py
.DS_Store<->/Users/greyzhang/Downloads/vim-autocomplpop
acp.vim<->/Users/greyzhang/Downloads/vim-autocomplpop/autoload
acp.jax<->/Users/greyzhang/Downloads/vim-autocomplpop/doc
acp.txt<->/Users/greyzhang/Downloads/vim-autocomplpop/doc
acp.vim<->/Users/greyzhang/Downloads/vim-autocomplpop/plugin
Although the structure and format are not pretty, but at least it can be used.