gpt4 book ai didi

Python 文件 list

转载 作者:行者123 更新时间:2023-12-01 21:22:46 25 4
gpt4 key购买 nike

我正在尝试查找以“msCam”开头并以扩展名“.avi”结尾的文件夹中的所有文件。我设法使用以下代码做到这一点:

path = path_to_analyze
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
'msCam' in i]
print(len(files))
for file in files:
if file.endswith(".avi"):
msFileList = [os.path.join(path, file)]
print(msFileList)

但这只存储在给定的“msFileList”变量中找到的最后一个文件。

print(msFileList)

如何传递所有要存储的文件?

View the output here

最佳答案

path = path_to_analyze
files = [i for i in os.listdir(path) if os.path.isfile(os.path.join(path,i)) and \
'msCam' in i]
msFileList = [] #create and empty list
print(len(files))
for file in files:
if file.endswith(".avi"):
msFileList.appened(os.path.join(path, file)) #append result to list

print(msFileList)

关于Python 文件 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63645081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com