FileHelpers
psengine.helpers.helpers.FileHelpers
¶
Helpers for file related functions.
read_csv
staticmethod
¶
Reads all rows from a CSV.
It is the client's responsibility to ensure column headers are handled appropriately.
Using as_dict will read the CSV with csv.DictReader, which treats the first row
as column headers. For example:
as_dict=True will return:
single_column=True will return:
as_dict=False and single_column=False will return:
| PARAMETER | DESCRIPTION |
|---|---|
csv_file
|
Path to CSV file.
TYPE:
|
as_dict
|
Return rows as dictionaries keyed by header.
TYPE:
|
single_column
|
Return only first column values as strings.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both |
ReadFileError
|
If file is not found or has restricted access. |
| RETURNS | DESCRIPTION |
|---|---|
list
|
List of rows from the CSV file. |
Source code in psengine/helpers/helpers.py
write_file
staticmethod
¶
Write string content to a file.
Creates the specified output directory if it doesn't exist. Overwrites file if it exists.
| PARAMETER | DESCRIPTION |
|---|---|
to_write
|
Content to write to file.
TYPE:
|
output_directory
|
Directory to write the file into.
TYPE:
|
fname
|
Name of the file to write.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If any of the parameters are invalid. |
WriteFileError
|
If the write operation fails. |
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Path to the file written. |