在Python中的特定文件夹名称后获取目录

问题描述 投票:0回答:1

我正在尝试在特定文件夹之后获取​​目录的其余部分。举例来说:

/ here / some / folders / with / files

而且我想要/ folders /之后的所有内容:

/ with / files

这是我所拥有的:

Tk().withdraw()
directory = os.path.basename(filedialog.askdirectory())
print(directory)

我知道basename只打印当前文件夹,但是我什至找不到任何内容来尝试对其进行编码。

python python-3.x tkinter python-os
1个回答
0
投票
也许您正在寻找此代码:

import os def getListOfFiles(dirName): # create a list of file and sub directories # names in the given directory listOfFile = os.listdir(dirName) allFiles = list() # Iterate over all the entries for entry in listOfFile: # 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): allFiles = allFiles + getListOfFiles(fullPath) else: allFiles.append(fullPath) return allFiles dirName = '/home/erfan/Downloads'

在给定路径下获取目录树中所有文件的列表

listOfFiles = getListOfFiles(dirName) print(listOfFiles)

© www.soinside.com 2019 - 2024. All rights reserved.