Load text from a text file or argument into a Python string variable - lvphj/epydemiology GitHub Wiki
Function to load text into a Python string either from a text file or from a passed function argument.
myStr = epy.phjGetStrFromArgOrFile(phjStr = None,
phjPathAndFileName = None,
phjAllowedAttempts = 3,
phjPrintResults = False)
Description
Function to load text into a Python string either from a text file or from a passed function argument. 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. The function is used internally to allow, for example, an SQL query to either be retrieved from a text file or to be passed as a function argument directly. It's hard to imagine why this function would be useful in an interactive context. When loading a string from a text file, this function calls the phjReadTextFromFile()
function; if you want to import text from a text file, you could use the phjGetStrFromArgOrFile()
function but it may be easier to use the phjReadTextFromFile()
function directly.
Either a string can be passed to a Python variable using the phjStr argument or a string can be retrieved from a text file, the path and name of which are given in the phjPathAndFileName argument. If both arguments are set, the string in the file is given preference. If the path and file name are incorrect, the function will prompt for a correct version. If, after the allowed attempts has been exceeded, the file can still not be located, the string in the phjStr argument will be used.
Exceptions
-
AssertionError if both phjStr and phjPathAndFileName arguments are equal to None or are not strings.
-
FileNotFoundError