编写一个程序,不使用“os.步行,但我只能遍历给定目录的第一个子目录,而不能遍历所有子目录。你知道吗
import os
sub_dir = []
fi = []
def print_directory_contents(sPath):
di = os.listdir(sPath)
for y in di:
if "." in y:
fi.append(y)
else:
sub_dir.append(y)
print("path : ", sPath)
print("dir : ", sub_dir)
print("file : ", fi)
print("--------------------------------\n")
sub_dir.clear()
fi.clear()
# print(di)
print(di)
for x in di:
print(di)
if os.path.isdir('{}/{}'.format(sPath, x)):
os.chdir('{}/{}'.format(sPath, x))
new_spath = os.getcwd()
print_directory_contents(new_spath)
check_dir = input()
print_directory_contents(check_dir)
递归调用函数:
相关问题 更多 >
编程相关推荐