Load text from a text file into a Python string variable - lvphj/epydemiology GitHub Wiki

Python function to read text a text file (e.g. SQL query or regular expression) into a Python string variable.

phjReadTextFromFile()

myStr = epy.phjReadTextFromFile(phjPathAndFileName = None,
                                phjMaxAttempts = 3,
                                phjPrintResults = False)

Description

This function can be used to read text from a text file. It allows the path to the file to be entered on-the-fly in the event that the file does not exist at the original entered location.

Function parameters

  1. phjFilePathAndName

    The full path and name of the text file to read. The text file does not need to end with the prefix '.txt'. This is the function that is used to read SQL queries from text files in the phjGetDataFromDatabase() function.

  2. phjMaxAttempts (default = 3)

    Number of times function will prompt for correct path and file name.

  3. phjPrintResults (default = False)

    Print the string retrieved from the file.

Exceptions raised

  1. FileNotFoundError

Returns

A string containing the contents of the text file.

Other notes

None.

Example

myStr = epy.phjReadTextFromFile(phjPathAndFileName = '/Users/username/Desktop/myTextFile.txt',
                                phjMaxAttempts = 3,
                                phjPrintResults = False)