目录中所有文件的列表?

有人能帮我创建一个函数,将创建一个名单下的所有文件的某个目录使用 pathlib库?

在这里,我有一个:

enter image description here

是的

  • c:\desktop\test\A\A.txt

  • c:\desktop\test\B\B_1\B.txt

  • c:\desktop\test\123.txt

我希望有一个单独的列表,其中包含上面的路径,但是我的代码返回一个嵌套列表。

这是我的代码:

from pathlib import Path


def searching_all_files(directory: Path):
file_list = [] # A list for storing files existing in directories


for x in directory.iterdir():
if x.is_file():


file_list.append(x)
else:


file_list.append(searching_all_files(directory/x))


return file_list




p = Path('C:\\Users\\akrio\\Desktop\\Test')


print(searching_all_files(p))

希望有人能纠正我。

161039 次浏览
def searching_all_files(directory: Path):
file_list = [] # A list for storing files existing in directories


for x in directory.iterdir():
if x.is_file():
file_list.append(x)#here should be appended
else:
file_list.extend(searching_all_files(directory/x))# need to be extended


return file_list

You can use os.listdir(). It will get you everything that's in a directory - files and directories.

如果只想要文件,可以使用 os.path 过滤:

from os import listdir
from os.path import isfile, join
onlyfiles = [files for files in listdir(mypath) if isfile(join(mypath, files))]

或者您可以使用 os.walk () ,它将为它访问的每个目录生成两个列表——为您分割成文件和目录。如果你只想要顶部的目录,你可以只打破它的第一次产生

from os import walk
files = []
for (dirpath, dirnames, filenames) in walk(mypath):
files.extend(filenames)
break
from pathlib import Path
from pprint import pprint


def searching_all_files(directory):
dirpath = Path(directory)
assert dirpath.is_dir()
file_list = []
for x in dirpath.iterdir():
if x.is_file():
file_list.append(x)
elif x.is_dir():
file_list.extend(searching_all_files(x))
return file_list


pprint(searching_all_files('.'))

Use Path.glob() to list all files and directories. And then filter it in a List Comprehensions.

p = Path(r'C:\Users\akrio\Desktop\Test').glob('**/*')
files = [x for x in p if x.is_file()]

更多关于 pathlib模块的信息:

import pathlib


def get_all_files(dir_path_to_search):
filename_list = []


file_iterator = dir_path_to_search.iterdir()


for entry in file_iterator:
if entry.is_file():
#print(entry.name)
filename_list.append(entry.name)


return filename_list

这个函数我们可以测试为-

dir_path_to_search= pathlib.Path("C:\\Users\\akrio\\Desktop\\Test")
print(get_all_files(dir_path_to_search))

使用 pathlib2要容易得多,

from pathlib2 import Path


path = Path("/test/test/")
for x in path.iterdir():
print (x)

如果文件具有相同的后缀,如 .txt,则可以使用 rglob递归地列出主目录和所有子目录。

paths = list(Path(INPUT_PATH).rglob('*.txt'))

如果需要对每个路径应用任何有用的 路径函数,例如,访问 name属性:

[k.name for k in Path(INPUT_PATH).rglob('*.txt')]

其中 INPUT_PATH是主目录的路径,而 Path是从 pathlib导入的。

如果可以假定只有文件对象的名称中有 .(即。短信。Png 等)你可以做一个全局搜索或者递归全局搜索..。

from pathlib import Path


# Search the directory
list(Path('testDir').glob('*.*'))


# Search directories and subdirectories, recursively
list(Path('testDir').rglob('*.*'))

But that's not always the case. Sometimes there are hidden directories like .ipynb_checkpoints and files that do not have extensions. In that case, use list comprehension or a filter to sort out the Path objects that are files.

# Search Single Directory
list(filter(lambda x: x.is_file(), Path('testDir').iterdir()))


# Search Directories Recursively
list(filter(lambda x: x.is_file(), Path('testDir').rglob('*')))
# Search Single Directory
[x for x in Path('testDir').iterdir() if x.is_file()]


# Search Directories Recursively
[x for x in Path('testDir').rglob('*') if x.is_file()]

A similar, more functional-oriented solution to @ prasastoadi’s one can be achieved by using the built-in filter function of Python:

from pathlib import Path


my_path = Path(r'C:\Users\akrio\Desktop\Test')
list(filter(Path.is_file, my_path.glob('**/*')))

With pathlib, it is as simple as the below comand.

path = Path('C:\\Users\\akrio\\Desktop\\Test')
list(path.iterdir())

定义目录路径:

from pathlib import Path
data_path = Path.home() / 'Desktop/My-Folder/'

Get all paths (files and directories):

paths = sorted(data_path.iterdir())

Get file paths only:

files = sorted(f for f in Path(data_path).iterdir() if f.is_file())

获取具有特定模式的路径(例如,使用. png 扩展名) :

png_files = sorted(data_path.glob('*.png'))

You can use a generator like this one with online filtering:

for file in (_ for _ in directory.iterdir() if _.is_file()):
...