此代码显示访问被拒绝问题。我不想使用 os.walk 函数来获取我想在没有它的情况下获取的所有文件和目录。
import os
import sys
test_path = "/"
def print_directory_contents(dir_path):
for child in os.listdir(dir_path):
path = os.path.join(dir_path, child)
if os.path.isdir(path):
print("FOLDER: " + "\t" + path)
print_directory_contents(path)
else:
print("FILE: " + "\t" + path)
print_directory_contents(test_path)
Output
Traceback (most recent call last):
File "a.py", line 33, in <module>
print_directory_contents(test_path)
File "a.py", line 26, in print_directory_contents
print_directory_contents(path)
File "a.py", line 26, in print_directory_contents
print_directory_contents(path)
File "a.py", line 22, in print_directory_contents
for child in os.listdir(dir_path):
PermissionError: [Errno 13] Permission denied: '/tmp/pulse-
PKdhtXMmr18n'