Python:Flexible Programming
This page covers some ways Python programs can be made more flexible by using strings and format specifiers.
Loading Various Files
If you have several files (in similar formats) that you need a script to load, rather than hard-coding each load, you can load the files in a loop. There are two fundamentally different ways to go about this:
- If the file names all have a pattern, you can use a formatted string to "build" the filename and then use that string with the appropriate loading command.
- If the files are all ni the same folder, or in a folder where it is easy to hand-code files to exclude from loading, you can use the
os
module in Python and specifically theos.listdir(PATH)
method to get a list of strings with the filenames at PATH.
Here is a Trinket demonstrating the latter; note that Trinket does not have the ability to create folders so main.py
and the two data files are all in the same place; the code on likes 12-13 causes the loop to skip past
any file names included in the list. If yuo aer able to put all your data files in a subfolder, that might work better - just adjust the path
variable with the relative path to that subfolder from where the main script is running.
Also, this program shows two different ways of loading files: with pandas and with numpy. See the Pandas page for more specific information on how to load data using pandas.