Python OS - 使用'os.listdir' 查看一个目录,即returns 文件夹列表。如何查看子文件夹的内容?
Python OS - using 'os.listdir' to view a directory that returns a list of folders. How do I view the contents of the subfolders?
我被设置了一个挑战,要求我浏览一个包含 mos 个空文件夹的目录 - flag(answer) 在其中一个文件夹中。我已经使用 os 模块查看所有文件夹的名称 - 它们都被命名为 'folder-' 加上 1 到 200 之间的数字。我如何查看其中的内容?
你应该使用 os.walk() 而不是像
这样的 litdir()
import os
import os.path
for dirpath, dirnames, filenames in os.walk("."):
for file in filenames:
print(file)
import os
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
File_list = os.listdir(dirName)
Files = list()
# Iterate over all the entries
for entry in File_list:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
Files = Files + getListOfFiles(fullPath)
else:
Files.append(fullPath)
return Files
#Call the above function to create a list of files in a directory tree i.e.
dirName = 'C:/Users/huzi95s/Desktop/Django';
# Get the list of all files in directory tree at given path
listOfFiles = getListOfFiles(dirName)
我被设置了一个挑战,要求我浏览一个包含 mos 个空文件夹的目录 - flag(answer) 在其中一个文件夹中。我已经使用 os 模块查看所有文件夹的名称 - 它们都被命名为 'folder-' 加上 1 到 200 之间的数字。我如何查看其中的内容?
你应该使用 os.walk() 而不是像
这样的 litdir()import os
import os.path
for dirpath, dirnames, filenames in os.walk("."):
for file in filenames:
print(file)
import os
def getListOfFiles(dirName):
# create a list of file and sub directories
# names in the given directory
File_list = os.listdir(dirName)
Files = list()
# Iterate over all the entries
for entry in File_list:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
Files = Files + getListOfFiles(fullPath)
else:
Files.append(fullPath)
return Files
#Call the above function to create a list of files in a directory tree i.e.
dirName = 'C:/Users/huzi95s/Desktop/Django';
# Get the list of all files in directory tree at given path
listOfFiles = getListOfFiles(dirName)