Python File Detection Loop

Issue

I am trying to create a Python script that loops through all files in a directory, and when it detects a file that starts with, as an example, TestFile, I’d like the loop to stop. My current attempt at this is either resulting in the loop continuing after finding the file, or ends the script after looping through the directory once. Any assistance would be greatly appreciated.

import os
import time

# Defining variables
dir_path = os.path.dirname(os.path.realpath(__file__))
timeout = 30  # seconds
timeoutStart = time.time()

# While loop that should last 15 minutes
# to search for any file that starts with
# CustomerInfo, then the loop breaks when
# a file is found.
while time.time() < timeoutStart + timeout:
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.startswith('TestFile'):
                file_export = root+'\\'+str(file)
                file_name = file
                print(file_export)
                print(file_name)
    break

Errors I now get after updating the code:

Traceback (most recent call last):
  File "/script/dir/FileTransfer.py", line 80, in <module>
    if find_file(dir_path, 'TestFile'):
  File "/script/dir/FileTransfer.py", line 26, in find_file
    send_email('<my email address>',
  File "/script/dir/FileTransfer.py", line 53, in send_email
    attachment = open(attachment_location, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'TestFile.txt'

NOTE: the script works when I am running it from my user’s directory, however it doesn’t work when I move the file to where I need the script to run.

Updated full script:

#!/usr/bin/env python3

import os
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path

# Defining variables
dir_path = os.path.dirname(os.path.realpath(__file__))
timeout = 900  # seconds
timeoutStart = time.time()


# Creating a function that loops through
# the directory searching for the file.
def find_file(dir_path, txt):
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.startswith(txt):
                print(os.path.join(root, file))
                print(file)
                send_email('<my email address',
                           '<subject>',
                           '<body>',
                           file_export)
                return True
    return False


# Creating a function that sends an email along
# with attaching the file found by the find_file
# function noted above.
def send_email(email_recipient,
               email_subject,
               email_message,
               attachment_location=''):

    email_sender = '<my email address>'

    msg = MIMEMultipart()
    msg['From'] = email_sender
    msg['To'] = email_recipient
    msg['Subject'] = email_subject

    msg.attach(MIMEText(email_message, 'plain'))

    if attachment_location != '':
        filename = os.path.basename(attachment_location)
        attachment = open(attachment_location, "rb")
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(attachment.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition',
                        "attachment; filename= %s" % filename)
        msg.attach(part)

    try:
        server = smtplib.SMTP('<email server>', <port>)
        server.ehlo()
        server.starttls()
        server.login('<server auth username>', '<server auth password>')
        text = msg.as_string()
        server.sendmail(email_sender, email_recipient, text)
        print('email sent')
        server.quit()
    except:
        print("SMTP server connection error")
    return True


# While loop that should last 15 minutes
# to search for any file that starts with
# CustomerInfo, then the loop breaks when
# a file is found.
while time.time() < timeoutStart + timeout:
    if find_file(dir_path, 'TestFile'):
        break


# Print statement for log output.
print(time.time())
print("End of script.")

I found my issue, and I reflected the code above for those who are interested. The email function requires the absolute path to the file, where I am only calling the file name (IE: under the send_email function attachment_location was set to file instead of file_export).

Solution

The simplest solution might be to create a function to do the searching using return to terminate the function when the file is found:

# Returns True only if the file is found
def find_file(dir_path, txt):
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.startswith(txt):
                print(os.path.join(root, file))
                print(file)
                return True
    return False

while time.time() < timeoutStart + timeout:
    if find_file(dir_path, 'TestFile'):
        break

Answered By – Johnny Mopp

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published