在Python中,如何只从一个目录的所有子目录中获取前50个文件?
我试图从特定目录的不同子目录中获取前50个.jpg文件。到目前为止,我只使用以下命令列表成功获取了所有的路径名:在Python中,如何只从一个目录的所有子目录中获取前50个文件?,python,file,path,Python,File,Path,我试图从特定目录的不同子目录中获取前50个.jpg文件。到目前为止,我只使用以下命令列表成功获取了所有的路径名: import os import os.path for dirpath, dirnames, filenames in os.walk("."): for filename in [f for f in filenames if f.endswith(".jpg")]: print os.path.join(dirpath, filename) 有人能告
import os
import os.path
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(".jpg")]:
print os.path.join(dirpath, filename)
有人能告诉我如何从子目录中只提取前50个文件吗?获取列表的第一个
n
值。如果只需要文件名,请在/
上拆分,并获取最后一个元素:
import os
import os.path
n = 50
fext = ".jpg"
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(fext)][:n]:
print (filename)
取列表中的第一个n
值。如果只需要文件名,请在/
上拆分,并获取最后一个元素:
import os
import os.path
n = 50
fext = ".jpg"
for dirpath, dirnames, filenames in os.walk("."):
for filename in [f for f in filenames if f.endswith(fext)][:n]:
print (filename)
使用os.listdir
列出文件和目录
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
使用os.path.isdir
获取目录
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
下面的代码获取每个子目录中50'.jpg文件的完整路径
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
使用os.listdir
列出文件和目录
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
使用os.path.isdir
获取目录
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
下面的代码获取每个子目录中50'.jpg文件的完整路径
target_dir = os.path.abspath('.')
sub_dir_list = [os.path.join(target_dir, one_dir) for one_dir in os.listdir(target_dir) if
os.path.isdir(os.path.join(target_dir, one_dir))]
img_file_list = []
for sub_dir in sub_dir_list:
img_file_list.extend([one_file for one_file in os.listdir(sub_dir) if one_file.endswith('.jpg')][:50])
最好的办法是避免
- 创建一个
列表
理解
- 使用简单的切片将其切片
[:50]
这会创建两个无用的列表,但没有那么好的性能
我建议使用生成器理解而不是列表理解来生成名称,并在其上使用islice
。这样就不会创建临时列表。一次只能使用1个值:
import itertools,os
for dirpath, dirnames, filenames in os.walk("."):
for filename in itertools.islice((f for f in filenames if f.endswith(".jpg")),50):
print(os.path.join(dirpath, filename))
还要小心:一些奇怪的文件系统不一定会按名称对文件进行排序,也许您需要先对列表进行排序,然后进行切片
for filename in itertools.islice(sorted([f for f in filenames if f.endswith(".jpg")]),50):
最好的办法是避免
- 创建一个
列表
理解
- 使用简单的切片将其切片
[:50]
这会创建两个无用的列表,但没有那么好的性能
我建议使用生成器理解而不是列表理解来生成名称,并在其上使用islice
。这样就不会创建临时列表。一次只能使用1个值:
import itertools,os
for dirpath, dirnames, filenames in os.walk("."):
for filename in itertools.islice((f for f in filenames if f.endswith(".jpg")),50):
print(os.path.join(dirpath, filename))
还要小心:一些奇怪的文件系统不一定会按名称对文件进行排序,也许您需要先对列表进行排序,然后进行切片
for filename in itertools.islice(sorted([f for f in filenames if f.endswith(".jpg")]),50):
您想要.log
还是.jpg
?您的代码是log
,您的问题描述是jpg
。对此表示抱歉。实际上是.jpgDo,您想要.log
还是.jpg
?您的代码是log
,您的问题描述是jpg
。对此表示抱歉。您想要的文件名实际上是.jpgif;只是打印(文件名)
是的,这是非常正确的;只是打印(文件名)
是的,非常正确。