1

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.

Lazerhorse
  • 123
  • 1
  • 8

2 Answers2

1

you can use a list comprehension:

list_final = [e for e in os.listdir(path) if e.endswith('.PDF')]

or you could use pathlib.Path.glob :

from pathlib import Path

p = Path(path)
list_final = [e.name for e in p.glob('*.PDF')]
kederrac
  • 15,932
  • 5
  • 29
  • 52
1

Try using glob.glob(), it will find all files that meet a pattern:

import glob
print(glob.glob("*.pdf"))  # returns a list of filenames

Or if you want to use another path than the current path, just join it to the pattern

print(glob.glob(path + "/*.pdf"))  # returns a list of filenames

Or even better, use os.path.join() instead:

from os.path import join
glob.glob(join(path, "/*.pdf"))
Iván C.
  • 939
  • 6
  • 14