it’s a Python newbie question (thanks to the post Import multiple excel files into python pandas and concatenate them into one dataframe)
The script is:
import os
files = os.listdir('C:\\TEST')
files_pdf = [f for f in files if f[-3:] == 'pdf']
print files_pdf
it give all the names of PDF files in the folder.
I am trying to understand it from a basic way. I guess the longest line above functions as:
files_ pdf = []
for f in files:
if f[-3:] == ‘pdf’:
files_ pdf.append(f)
the question is that, what's the difference? and what’s the reason or principle of the ‘f for f in files’?
[for f in files if f[-3:] == 'pdf'] #doesn't work
[f for f in files if f[-3:] == 'pdf'] #works
thanks.