0

I have a problem for searching in a folder to find if a file name exists. It specifically has to be able to read a PDF filename. I have foud the following

import os
os.path.exists('memo.txt')

How ever I have no clue how to use it. I have dropbox installed and I will primarlily be searching the folders there for a specific PDF file. I will need it to return true if it is there as it will be attahced to an email scriopt that I am working on.

WAMPS
  • 329
  • 1
  • 6
  • 20
  • Possible duplicate of [Find all files in directory with extension .txt with python](http://stackoverflow.com/questions/3964681/find-all-files-in-directory-with-extension-txt-with-python) – oystein-hr Jan 15 '16 at 19:15

2 Answers2

1

You could create a list with all the folders in the direcory:

import os
fileDir = os.listdir(directory) #I hope that isn't a keyword
print(any(x==fileName for x in fileDir))
#will return True if the file is in the directory

Something like that should do the trick. You could put that inside a function if you like so that it returns True instead of printing it.

T Bell
  • 96
  • 4
0

I would suggest that you use Python's built in library pathlib

from pathlib import Path
dir_path = Path('/your/path/to/directory')
# get all pdf files in directory
pdf_files = dir_path.glob('*.pdf') # it returns a list
# you can now check if specific name exists in the list
for pdf_file in pdf_files:
    if 'your_file_name' in pdf_file.name:
        return True

I hope this helps

Bob Ezuba
  • 510
  • 1
  • 5
  • 22