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
-
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 thephjGetDataFromDatabase()
function. -
phjMaxAttempts (default = 3)
Number of times function will prompt for correct path and file name.
-
phjPrintResults (default = False)
Print the string retrieved from the file.
Exceptions raised
- FileNotFoundError
Returns
A string containing the contents of the text file.
Other notes
None.
Example
A .txt
file in the epydemiology folder entitled myEpydemiologyTestFile.txt
contains the text 'String to test ePydemiology function'.
myStr = epy.phjReadTextFromFile(phjPathAndFileName = './myEpydemiologyTestFile.txt',
phjMaxAttempts = 3,
phjPrintResults = False)
String to test ePydemiology function.