I have a code that outputs all of the files that are a .pdf in a directory. It outputs a stack of strings like below.
file0.PDF
file1.PDF
file2.PDF
file3.PDF
I want to put these strings into a list, that looks like this:
['file0.PDF', 'file1.PDF', 'file2.PDF', 'file3.PDF']
I have managed to do this with the code below.
import os
list_final = []
for file in os.listdir(path):
if ".PDF" in file:
for value in file.split('\n'):
list_final.append(value)
print(list_final)
This gives the list in the format above, which is what I want.
Is there a better way to do this? I feel that my code is very inefficient. I have tried through a list comprehensions such as the below but I am unsure why it does not work.
list_final = [value for value in file.split('\n')]
Thanks in advance.