kunaljubce commented on code in PR #62321:
URL: https://github.com/apache/airflow/pull/62321#discussion_r2882730749


##########
providers/imap/src/airflow/providers/imap/hooks/imap.py:
##########
@@ -310,11 +318,24 @@ def _correct_path(self, name: str, 
local_output_directory: str) -> str:
             else local_output_directory + "/" + name
         )
 
-    def _create_file(self, name: str, payload: Any, local_output_directory: 
str) -> None:
-        file_path = self._correct_path(name, local_output_directory)
-
-        with open(file_path, "wb") as file:
-            file.write(payload)
+    def _create_file(
+        self, name: str, payload: Any, local_output_directory: str, 
overwrite_file: bool
+    ) -> None:
+        if overwrite_file:
+            method = "wb"
+        else:
+            method = "xb"
+            filename, extension = os.path.splitext(name)
+            counter = 1
+        while True:
+            file_path = self._correct_path(name, local_output_directory)
+            try:
+                with open(file_path, method) as file:
+                    file.write(payload)
+                break
+            except FileExistsError:
+                name = f"{filename}_{counter}{extension}"
+                counter += 1

Review Comment:
   @vaefremov95 I have a concern with the logic implemented here. 
   
   1. So if `overwrite_file` defaults to `True`, `method = "wb"` is set. 
   2. `while` loop starts -> tries to write the `payload` to a file -> runs 
into `FileExistsError`
   3. Tries to increment `counter` by 1. But where's `counter` initialised in 
this flow?
   4. Also if you initialise `counter` above the `if overwrite_file` block`, 
does the `while` loop run into an infinite loop? I guess I don't understand the 
purpose of your `while` loop here 😕 
   
   Happy to discuss!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to