reading file content in python

Viewed 22

I have a directory with scripts containing some content that I want to print out. I only want to print contents from the scripts whose names are in the list on line 2 of the code below. The script names are super long (for eg - file_1_08022022_2350.txt), hence I am using the startswith() function to identify the scripts.

Here is what I came up with. This however does not print out anything. Will appreciate any help.

import os
file_names = [file_1, file_3, file_4]
os.chdir(path_to_folder_with_scripts)
for i in file_names:
   for filename in os.listdir(os.getcwd())
      if filename.startswith(i):
         with open(filename, 'r') as file:
             print(file)
             print('---------------------------------------')
0 Answers
Related