ldacey
ldacey

Reputation: 578

Checking if a list of files exists before proceeding?

I have some pandas code running for 9 different files each day. Currently, I have a scheduled task to run the code at a certain time but sometimes the files have not been uploaded to the SFTP by our client on time which means that the code will fail. I want to create a file checking script.

Upvotes: 8

Views: 11030

Answers (3)

Sabito
Sabito

Reputation: 5065

Another much simpler approach using map:

import os

file_names_list = ['file1', 'file2', 'file3']

if all(list(map(os.path.isfile,file_names_list))):
   # do something
else:
   # do something else!

Upvotes: 5

TheF1rstPancake
TheF1rstPancake

Reputation: 2378

Shortening Farhan's answer. You can use list comprehension and be extra fancy to simplify the code.

import os, time
while True:
   filelist = ['file1', 'file2', 'file3']
   if all([os.path.isfile(f) for f in filelist]):
      break
   else:
      time.sleep(600)

Upvotes: 10

Farhan.K
Farhan.K

Reputation: 3485

import os, time

filelist = ['file1','file2','file3']

while True:
    list1 = []

    for file in filelist:
        list1.append(os.path.isfile(file))

    if all(list1):
        # All elements are True. Therefore all the files exist. Run %run commands
        break
    else:
        # At least one element is False. Therefore not all the files exist. Run FTP commands again
        time.sleep(600) # wait 10 minutes before checking again

all() checks if all the elements in the list are True. If at least one element is False it returns False.

Upvotes: 9

Related Questions