Skip to content

Commit 822dffc

Browse files
committed
fix win related error
1 parent b69fad1 commit 822dffc

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

pandas/io/common.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
BaseBuffer,
6060
ReadCsvBuffer,
6161
)
62+
from pandas.compat import is_platform_windows
6263
from pandas.compat._optional import import_optional_dependency
6364
from pandas.util._decorators import doc
6465
from pandas.util._exceptions import find_stack_level
@@ -1288,6 +1289,17 @@ def dedup_names(
12881289
return names
12891290

12901291

1292+
def _infer_protocol(path: str) -> str:
1293+
# Treat Windows drive letters like C:\ as local file paths
1294+
if is_platform_windows() and re.match(r"^[a-zA-Z]:[\\/]", path):
1295+
return "file"
1296+
1297+
parsed = parse_url(path)
1298+
if parsed.scheme in _VALID_URLS:
1299+
return parsed.scheme
1300+
return "file"
1301+
1302+
12911303
def _match_file(
12921304
path: Path | PurePosixPath, extensions: set[str] | None, glob: str | None
12931305
) -> bool:
@@ -1351,15 +1363,11 @@ def iterdir(
13511363
else:
13521364
extensions = {ext.lower() for ext in extensions}
13531365

1354-
if isinstance(path, os.PathLike):
1355-
path = os.fspath(path)
1356-
1357-
parsed = parse_url(path)
1358-
scheme = parsed.scheme or "file"
1359-
base_path = parsed.path if scheme == "file" else path
1366+
path_str = os.fspath(path)
1367+
scheme = _infer_protocol(path_str)
13601368

13611369
if scheme == "file":
1362-
resolved_path = Path(base_path)
1370+
resolved_path = Path(path_str)
13631371
if resolved_path.is_file():
13641372
if _match_file(
13651373
resolved_path,
@@ -1397,7 +1405,7 @@ def iterdir(
13971405
yield path_obj
13981406
return
13991407
if not fs.isdir(path):
1400-
raise NotADirectoryError(f"Remote path {path!r} is not a directory.")
1408+
raise NotADirectoryError(f"Path {path!r} is neither a file nor a directory.")
14011409

14021410
files = fs.ls(path, detail=True)
14031411
for f in files:

pandas/tests/io/parser/test_directory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def directory_data_to_file(tmp_path, directory_data):
2121
for data in data_list:
2222
file_name = next(iter(data.keys()))
2323
path = tmp_path / f"{file_name}.csv"
24-
with path.open("w", newline="") as file:
24+
with path.open("w", newline="", encoding="utf-8") as file:
2525
writer = DictWriter(file, fieldnames=field_names)
2626
writer.writeheader()
2727
writer.writerow(data[file_name])

0 commit comments

Comments
 (0)