udim commented on a change in pull request #14431:
URL: https://github.com/apache/beam/pull/14431#discussion_r616963405



##########
File path: examples/notebooks/tour-of-beam/reading-and-writing-data.ipynb
##########
@@ -724,16 +740,21 @@
         "    self.batch_size = batch_size\n",
         "\n",
         "  @staticmethod\n",
+        "  def expand_pattern(pattern):\n",
+        "    for match_result in beam_fs.match([pattern])[0].metadata_list:\n",
+        "      yield match_result.path\n",
+        "\n",
+        "  @staticmethod\n",
         "  def write_file(lines, file_name_prefix, file_name_suffix):\n",
         "    file_name = 
f\"{file_name_prefix}-{uuid.uuid4().hex}{file_name_suffix}\"\n",
-        "    with open(file_name, 'w') as f:\n",
+        "    with beam_fs.create(file_name) as f:\n",
         "      for line in lines:\n",
-        "        f.write(f\"{line}\\n\")\n",
+        "        f.write(f\"{line}\\n\".encode('utf-8'))\n",
         "\n",
         "  def expand(self, pcollection):\n",
         "    # Remove existing files matching the output file_name pattern.\n",
-        "    for f in 
glob.glob(f\"{self.file_name_prefix}*{self.file_name_suffix}\"):\n",
-        "      os.remove(f)\n",
+        "    for path in 
self.expand_pattern(f\"{self.file_name_prefix}*{self.file_name_suffix}\"):\n",

Review comment:
       Similarly, `WriteToText` might be used to simplify things.




-- 
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.

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


Reply via email to