Skip to content

Commit 8c916a7

Browse files
author
Alan Christie
committed
- Adds a pick_csv() to file_utils
1 parent 3c7bb96 commit 8c916a7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/python/pipelines_utils/file_utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,36 @@ def pick_sdf(filename, directory=None):
8181
return file_path + '.sdf'
8282
# Couldn't find a suitable SDF file
8383
return None
84+
85+
86+
def pick_csv(filename, directory=None):
87+
"""Returns a full path to the chosen CSV file. The supplied file
88+
is not expected to contain a recognised CSV extension, this is added
89+
automatically.
90+
If a file with the extension `.csv.gz` or `.csv` is found the path to it
91+
(excluding the extension) is returned. If this fails, `None` is returned.
92+
93+
:param filename: The CSV file basename, whose path is required.
94+
:type filename: ``str``
95+
:param directory: An optional directory.
96+
If not provided it is calculated automatically.
97+
:type directory: ``str``
98+
:return: The full path to the file without extension,
99+
or None if it does not exist
100+
:rtype: ``str``
101+
"""
102+
if directory is None:
103+
directory = utils.get_undecorated_calling_module()
104+
# If the 'cwd' is not '/output' (which indicates we're in a Container)
105+
# then remove the CWD and the anticipated '/'
106+
# from the front of the module
107+
if os.getcwd() not in ['/output']:
108+
directory = directory[len(os.getcwd()) + 1:]
109+
110+
file_path = os.path.join(directory, filename)
111+
if os.path.isfile(file_path + '.csv.gz'):
112+
return file_path + '.csv.gz'
113+
elif os.path.isfile(file_path + '.csv'):
114+
return file_path + '.csv'
115+
# Couldn't find a suitable CSV file
116+
return None

0 commit comments

Comments
 (0)