简体   繁体   中英

Iterate over first n files of file list

file_list is the folder path containing a list of files.

I want to do certain action on the files inside file_list . To perform the action on all the files, here is the python code;

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. How do I modify the code? I am open to totally new code to do the task.

I am using python v3.6

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

Is it suitable for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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