0

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
  • 10

2 Answers2

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"))
  • you can just do `glob.glob(path + "/*.pdf")`, or even better, use [os.path.combine](https://docs.python.org/3.8/library/os.path.html#os.path.join) –  Apr 11 '20 at 22:41
  • now looks better, + 1 – kederrac Apr 11 '20 at 23:02
0

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
  • 16,819
  • 6
  • 32
  • 55