import os
def __file_list__(path, level):
files = os.listdir(path);
for i in files:
path_tmp = path + i;
if True == os.path.isdir(path_tmp):
print("%s[DIR] %s" % (level_flag * level, path_tmp));
__file_list__(path_tmp + "/", level + 1);
else:
print("%s[FILE] %s" % (level_flag * level, path_tmp));
pass
def file_list(path):
level = 0;
global level_flag; # 定义为全局变量
level_flag = "\t";
__file_list__(path, level);
pass
def main():
path = "E:/workspace/Python/201802/";
file_list(path);
pass
main();
output:
E:\workspace\Python\201802\venv\Scripts\python.exe E:/workspace/Python/201802/py_study/fileList.py
[DIR] E:/workspace/Python/201802/.idea
[FILE] E:/workspace/Python/201802/.idea/201802.iml
[DIR] E:/workspace/Python/201802/.idea/inspectionProfiles
[FILE] E:/workspace/Python/201802/.idea/misc.xml
[FILE] E:/workspace/Python/201802/.idea/modules.xml
[FILE] E:/workspace/Python/201802/.idea/webResources.xml
[FILE] E:/workspace/Python/201802/.idea/workspace.xml
[FILE] E:/workspace/Python/201802/execute.py
[FILE] E:/workspace/Python/201802/Perceptron.py
[DIR] E:/workspace/Python/201802/py_study
[FILE] E:/workspace/Python/201802/py_study/fileList.py
[FILE] E:/workspace/Python/201802/py_study/file_demo.py
[FILE] E:/workspace/Python/201802/py_study/helloWorld.py
[FILE] E:/workspace/Python/201802/test_data.csv
[DIR] E:/workspace/Python/201802/venv
[DIR] E:/workspace/Python/201802/venv/Include
[FILE] E:/workspace/Python/201802/venv/Include/abstract.h
[FILE] E:/workspace/Python/201802/venv/Include/accu.h
[FILE] E:/workspace/Python/201802/venv/Include/asdl.h
[FILE] E:/workspace/Python/201802/venv/Include/ast.h
[FILE] E:/workspace/Python/201802/venv/Include/bitset.h
[FILE] E:/workspace/Python/201802/venv/Include/bltinmodule.h
[FILE] E:/workspace/Python/201802/venv/Include/boolobject.h
[FILE] E:/workspace/Python/201802/venv/Include/bytearrayobject.h
[FILE] E:/workspace/Python/201802/venv/Include/bytesobject.h
[FILE] E:/workspace/Python/201802/venv/Include/bytes_methods.h
[FILE] E:/workspace/Python/201802/venv/Include/cellobject.h
[FILE] E:/workspace/Python/201802/venv/Include/ceval.h
[FILE] E:/workspace/Python/201802/venv/Include/classobject.h
[FILE] E:/workspace/Python/201802/venv/Include/code.h
[FILE] E:/workspace/Python/201802/venv/Include/codecs.h
[FILE] E:/workspace/Python/201802/venv/Include/compile.h
[FILE] E:/workspace/Python/201802/venv/Include/complexobject.h
[FILE] E:/workspace/Python/201802/venv/Include/datetime.h
[FILE] E:/workspace/Python/201802/venv/Include/descrobject.h
[FILE] E:/workspace/Python/201802/venv/Include/dictobject.h
[FILE] E:/workspace/Python/201802/venv/Include/dtoa.h
[FILE] E:/workspace/Python/201802/venv/Include/dynamic_annotations.h
[FILE] E:/workspace/Python/201802/venv/Include/enumobject.h
[FILE] E:/workspace/Python/201802/venv/Include/errcode.h
[FILE] E:/workspace/Python/201802/venv/Include/eval.h
[FILE] E:/workspace/Python/201802/venv/Include/fileobject.h
[FILE] E:/workspace/Python/201802/venv/Include/fileutils.h
[FILE] E:/workspace/Python/201802/venv/Include/floatobject.h
[FILE] E:/workspace/Python/201802/venv/Include/frameobject.h
[FILE] E:/workspace/Python/201802/venv/Include/funcobject.h
[FILE] E:/workspace/Python/201802/venv/Include/genobject.h
[FILE] E:/workspace/Python/201802/venv/Include/graminit.h
[FILE] E:/workspace/Python/201802/venv/Include/grammar.h
[FILE] E:/workspace/Python/201802/venv/Include/import.h
[FILE] E:/workspace/Python/201802/venv/Include/intrcheck.h
[FILE] E:/workspace/Python/201802/venv/Include/iterobject.h
[FILE] E:/workspace/Python/201802/venv/Include/listobject.h
[FILE] E:/workspace/Python/201802/venv/Include/longintrepr.h
[FILE] E:/workspace/Python/201802/venv/Include/longobject.h
[FILE] E:/workspace/Python/201802/venv/Include/marshal.h
[FILE] E:/workspace/Python/201802/venv/Include/memoryobject.h
[FILE] E:/workspace/Python/201802/venv/Include/metagrammar.h
[FILE] E:/workspace/Python/201802/venv/Include/methodobject.h
[FILE] E:/workspace/Python/201802/venv/Include/modsupport.h
[FILE] E:/workspace/Python/201802/venv/Include/moduleobject.h
[FILE] E:/workspace/Python/201802/venv/Include/namespaceobject.h
[FILE] E:/workspace/Python/201802/venv/Include/node.h
[FILE] E:/workspace/Python/201802/venv/Include/object.h
[FILE] E:/workspace/Python/201802/venv/Include/objimpl.h
[FILE] E:/workspace/Python/201802/venv/Include/odictobject.h
[FILE] E:/workspace/Python/201802/venv/Include/opcode.h
[FILE] E:/workspace/Python/201802/venv/Include/osdefs.h
[FILE] E:/workspace/Python/201802/venv/Include/osmodule.h
[FILE] E:/workspace/Python/201802/venv/Include/parsetok.h
[FILE] E:/workspace/Python/201802/venv/Include/patchlevel.h
[FILE] E:/workspace/Python/201802/venv/Include/pgen.h
[FILE] E:/workspace/Python/201802/venv/Include/pgenheaders.h
[FILE] E:/workspace/Python/201802/venv/Include/pyarena.h
[FILE] E:/workspace/Python/201802/venv/Include/pyatomic.h
[FILE] E:/workspace/Python/201802/venv/Include/pycapsule.h
[FILE] E:/workspace/Python/201802/venv/Include/pyconfig.h
[FILE] E:/workspace/Python/201802/venv/Include/pyctype.h
[FILE] E:/workspa