Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Cavitation-Analysis
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Elijah Andrews
Cavitation-Analysis
Commits
729ef092
Commit
729ef092
authored
Jun 22, 2020
by
Elijah Andrews
Browse files
Options
Downloads
Patches
Plain Diff
Added documentation to file utility functions.
parent
6b3aea8a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/util/file_utils.py
+39
-0
39 additions, 0 deletions
common/util/file_utils.py
with
39 additions
and
0 deletions
common/util/file_utils.py
+
39
−
0
View file @
729ef092
...
...
@@ -4,8 +4,18 @@ from PyQt5.QtWidgets import QFileDialog, QApplication
def
lists_to_csv
(
file_dir
,
filename
,
lists
,
headers
=
None
,
overwrite
=
False
):
"""
Writes a list of lists to a CSV file where each list is a column
:param file_dir: The directory in which to save the CSV file.
:param filename: The name of the CSV file (including file extension).
:param lists: The lists to save.
:param headers: A list of header strings, one for each column, defaults to no headers.
:param overwrite: Whether to overwrite an existing file, defaults to false.
:return: Success of file write.
"""
if
os
.
path
.
exists
(
file_dir
+
filename
)
and
not
overwrite
:
print
(
f
"
Warning: file already exists.
{
file_dir
+
filename
}
"
)
return
False
if
file_dir
!=
""
:
os
.
makedirs
(
file_dir
,
exist_ok
=
True
)
file
=
open
(
file_dir
+
filename
,
"
w
"
)
...
...
@@ -23,9 +33,17 @@ def lists_to_csv(file_dir, filename, lists, headers=None, overwrite=False):
line
=
line
[:
-
1
]
+
"
\n
"
file
.
write
(
line
)
file
.
close
()
return
True
def
csv_to_lists
(
file_dir
,
filename
,
has_headers
=
False
):
"""
Reads a CSV file to a list of lists where each column becomes a list.
:param file_dir: The directory from which to read the CSV file.
:param filename: The name of the CSV file (including file extension).
:param has_headers: Whether the CSV file has headers, defaults to False.
:return: List of lists.
"""
file
=
open
(
file_dir
+
filename
,
"
r
"
)
lines
=
file
.
readlines
()
start_idx
=
1
if
has_headers
else
0
...
...
@@ -40,6 +58,13 @@ def csv_to_lists(file_dir, filename, has_headers=False):
def
select_file
(
start_path
,
create_window
=
True
):
"""
Opens a file selection dialog.
:param start_path: The path at which to open the dialog.
:param create_window: Whether to create a new QApplication. May need to be set to false if QT is used elsewhere,
such as in matplotlib.
:return: String of the full file path.
"""
if
create_window
:
window
=
QApplication
([])
file_path
=
str
(
QFileDialog
.
getOpenFileName
(
None
,
"
Select File
"
,
start_path
)[
0
])
...
...
@@ -47,6 +72,13 @@ def select_file(start_path, create_window=True):
def
select_multiple_files
(
start_path
,
create_window
=
True
):
"""
Opens a multiple file selection dialog.
:param start_path: The path at which to open the dialog.
:param create_window: Whether to create a new QApplication. May need to be set to false if QT is used elsewhere,
such as in matplotlib.
:return: A list of full file path strings.
"""
if
create_window
:
window
=
QApplication
([])
file_paths
=
QFileDialog
.
getOpenFileNames
(
None
,
"
Select File
"
,
start_path
)[
0
]
...
...
@@ -54,6 +86,13 @@ def select_multiple_files(start_path, create_window=True):
def
select_dir
(
start_path
,
create_window
=
True
):
"""
Opens a directory selection dialog.
:param start_path: The path at which to open the dialog.
:param create_window: Whether to create a new QApplication. May need to be set to false if QT is used elsewhere,
such as in matplotlib.
:return: A list of full file path strings.
"""
if
create_window
:
window
=
QApplication
([])
dir_path
=
str
(
QFileDialog
.
getExistingDirectory
(
None
,
"
Select Directory
"
,
start_path
))
+
"
/
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment