简体   繁体   English

遍历文件列表的前n个文件

[英]Iterate over first n files of file list

file_list is the folder path containing a list of files. file_list是包含文件列表的文件夹路径。

I want to do certain action on the files inside file_list . 我想对file_list的文件执行某些操作。 To perform the action on all the files, here is the python code; 要对所有文件执行操作,这是python代码;

import os
for filename in os.listdir(file_list):
    print(filename)

What if I only want to perform the action on the first n files. 如果我只想对前n文件执行操作,该怎么办。 How do I modify the code? 如何修改代码? I am open to totally new code to do the task. 我愿意使用全新的代码来完成任务。

I am using python v3.6 我正在使用python v3.6

import os
for filename in os.listdir(file_list)[:n]:
    print(filename)

Is it suitable for you? 它适合你吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM