Skip to content

Conversation

jkgoodrich
Copy link
Contributor

@jkgoodrich jkgoodrich commented Sep 28, 2022

Add check_file_exists_raise_error to use file_exists to check the existence of all files in a list of file paths with options to raise an error.

Check whether all files in the list exist and raise an exception if any of them exist and overwrite is False.
:param fname_list: List of file paths to check the existence of.
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources exist.
Copy link
Contributor

@averywpx averywpx Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources exist.
:param overwrite: Optional boolean that will raise an exception if set to False and any of the resources doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above

:param fname: File name
:return: Whether the file exists
:param fname: File name.
:param overwrite: Optional boolean that will raise an error if set to False and file exists.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param overwrite: Optional boolean that will raise an error if set to False and file exists.
:param overwrite: Optional boolean that will raise an error if set to False and file doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually in this case I want to raise the error if it does exist. Use case is when the file exists and we are trying to overwrite it, but we forgot to specify overwrite=True

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I just want to check if any file in the list doesn't exist and which are they? Setting overwrite to False to figure out make thing complicated. This question may be asked more, because the function doesn't overwrite anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function isn't for a list, it's just for a single file. By default the overwrite parameter is None and will not be used at all, this will then raise no error, it will simply do what it did before and return True or False. I will change to make another function that calls this one and has a clearer name that includes overwrite, I was just trying to put it into a single function. For the other function you again don't need to pass in overwrite.

@jkgoodrich jkgoodrich requested a review from averywpx September 28, 2022 19:57
) -> bool:
"""
Check whether all files in the list exist and raise an exception if any of them exist and overwrite is False.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that the overwrite parameter needs use case explanation, since overwrite is kind of out of the functions scope (just to check if file in list exists) and it causes some confusion. Maybe just me, so it's up to you :)

Suggested change
The function will optionally raise an error if `overwrite` is set to False and any file exists, when the the function is called to check if the new created file paths exist.

@jkgoodrich jkgoodrich requested a review from averywpx September 28, 2022 20:52
@jkgoodrich jkgoodrich changed the title Add more file types to file_exists for checking '_SUCCESS' and add an 'overwrite' option to throw and exception if set to False and file exists.. Add more file types to file_exists for checking '_SUCCESS' Sep 28, 2022
Copy link
Contributor

@averywpx averywpx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add some suggestions so that the functions can be used in other cases as well. It's up to you if you want to change it :)


def check_file_exists_no_overwrite(
fname: Union[str, List[str]],
overwrite: bool = True,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are good reasons to raise errors when file exists or not, maybe it's good to let the user choose when to raise error. For example, raise_error can be "exist" or "not exist".

Suggested change
overwrite: bool = True,
def file_exists(fname: str, raise_error: Optional[bool] ="exist") -> bool:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding in something with more flexibility, let me know if that works

Comment on lines 151 to 155
for f in fname:
exists = file_exists(f)
all_exist &= exists
if exists and overwrite is False:
data_exceptions.append(f)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for f in fname:
exists = file_exists(f)
all_exist &= exists
if exists and overwrite is False:
data_exceptions.append(f)
for f in fname:
exists = file_exists(f)
all_exist &= exists
if exists and raise_error == "exist":
data_exceptions.append(f)
elif not exist and raise_error == "not exist":
data_exceptions.append(f)

Comment on lines 157 to 160
if data_exceptions:
raise DataException(
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if data_exceptions:
raise DataException(
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}"
)
if data_exceptions:
if raise_error == "exist":
raise DataException(
f"The following files already exist and the overwrite option was not used! {','.join(data_exceptions)}"
)
elif raise_error == "not exist":
raise DataException(
f"The following files doesn't exist! {','.join(data_exceptions)}"
)

@jkgoodrich jkgoodrich requested a review from averywpx September 28, 2022 21:49
Copy link
Contributor

@averywpx averywpx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one single comment!

@jkgoodrich jkgoodrich merged commit eafaaa4 into main Sep 29, 2022
@jkgoodrich jkgoodrich deleted the jg/mod_file_exists branch September 29, 2022 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants