Viranshul-P opened a new issue, #41618:
URL: https://github.com/apache/beam/issues/41618

   ### What happened?
   
   On Windows, a file pattern that uses `/` as the separator and contains a 
wildcard matches no files in the local filesystem. So 
`ReadFromText('data/*.txt')` fails with `OSError: No files found based on the 
file pattern data/*.txt`, even though the files exist. The same pattern written 
with `\` works, and so does a forward-slash path without a wildcard.
   
   Forward slashes are common on Windows: relative patterns like `data/*.txt` 
from examples, `pathlib.Path.as_posix()`, and paths read from config files. 
Python's own `glob.glob('data/*.txt')` finds the files.
   
   Repro (Windows 11, Python 3.12; same on Beam 2.78.0 from PyPI and on master 
at `dff63136a00`):
   
   ```python
   import os
   import tempfile
   
   import apache_beam as beam
   from apache_beam.io.filesystems import FileSystems
   
   os.chdir(tempfile.mkdtemp())
   os.makedirs('data')
   for name in ['a.txt', 'b.txt']:
     with open(os.path.join('data', name), 'w') as f:
       f.write('x\n')
   
   for pattern in ['data/*.txt', 'data\\*.txt', 'data/a.txt']:
     print(pattern, [m.path for m in 
FileSystems.match([pattern])[0].metadata_list])
   # data/*.txt []
   # data\*.txt ['data\\a.txt', 'data\\b.txt']
   # data/a.txt ['data/a.txt']
   
   with beam.Pipeline() as p:
     p | beam.io.ReadFromText('data/*.txt')
   # OSError: No files found based on the file pattern data/*.txt
   ```
   
   Absolute patterns behave the same (`C:/Users/me/data/*.txt` matches nothing, 
`C:\Users\me\data\*.txt` works). This affects everything that goes through 
`FileSystems.match`. `ReadFromText` and `ReadFromCsv` fail with "No files 
found", but `ReadAllFromText` and `fileio.MatchFiles` raise nothing and 
silently produce an empty `PCollection`, so the pipeline succeeds with no data.
   
   **Cause:** `LocalFileSystem._list` builds each listed path with 
`os.path.join`, so on Windows the file name is joined with `\` (`data\a.txt`). 
`FileSystem.match_files` then matches that path against the regex from 
`translate_pattern`, in which a literal `/` only matches `/`. `*` already 
accepts both separators (`[^/\\]*`), but the separators written in the pattern 
do not.
   
   **Proposed fix:** in `LocalFileSystem`, override `match_files` so that, when 
`os.path.altsep` is set (Windows), the pattern and the listed paths are both 
compared with `os.sep` only. The returned `FileMetadata` paths are unchanged, 
and other platforms and filesystems are unaffected. I have this ready with two 
regression tests (a real directory matched with forward slashes, and a 
`match_files` test with the Windows separators mocked so it also runs on 
Linux). Both fail on master and pass with the fix; the existing IO tests show 
no new failures.
   
   I'd like to work on this.
   
   This issue was drafted with help from Claude Code.
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [x] Component: IO connector
   


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