Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of pdf files in folder

Tags:

python

I want get a list of files name of all pdf files in folder I have my python script.

Now I have this code:

files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:

e = (len(files) - 1)

The problem are this code found all files in folder(include .py) so I "fix" if my script is the last file on the folder (zzzz.py) and later I subtract the last file of the list that are my script.py.

I try many codes for only find .pdf but this the more near I am.

like image 702
Xavier Villafaina Avatar asked Nov 30 '15 12:11

Xavier Villafaina


1 Answers

Use the glob module:

>>> import glob
>>> glob.glob("*.pdf")
>>> ['308301003.pdf', 'Databricks-how-to-data-import.pdf', 'emr-dg.pdf', 'gfs-sosp2003.pdf']
like image 93
vy32 Avatar answered Sep 21 '22 19:09

vy32