Skip to content

Commit fe9713e

Browse files
committed
Minor refactoring and reformatting
1 parent 0cfa522 commit fe9713e

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

pandas/io/common.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
import mmap
1919
import os
20+
from pathlib import Path
2021
from typing import (
2122
IO,
2223
Any,
@@ -519,19 +520,19 @@ def infer_compression(
519520
raise ValueError(msg)
520521

521522

522-
def check_parent_directory(fpath: str):
523+
def check_parent_directory(path: Path | str) -> None:
523524
"""
524525
Check if parent directory of a file exists, raise OSError if it does not
525526
526527
Parameters
527528
----------
528-
fpath: str
529+
path: Path | str
529530
File path
530531
531532
"""
532-
dirname = os.path.dirname(fpath)
533-
if len(dirname) != 0 and not os.path.isdir(dirname):
534-
raise OSError(fr"Cannot save file into a non-existent directory: '{dirname}'")
533+
parent = Path(path).parent
534+
if not parent.is_dir():
535+
raise OSError(fr"Cannot save file into a non-existent directory: '{parent}'")
535536

536537

537538
def get_handle(
@@ -646,9 +647,9 @@ def get_handle(
646647
compression_args = dict(ioargs.compression)
647648
compression = compression_args.pop("method")
648649

649-
# GH 24306
650-
if mode not in ["r", "rb"] and is_path: # Only for write methods
651-
check_parent_directory(str(handle))
650+
# Only for write methods
651+
if "r" not in mode and is_path:
652+
check_parent_directory(handle)
652653

653654
if compression:
654655
# compression libraries do not like an explicit text-mode

pandas/io/formats/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ def get_buffer(buf: FilePathOrBuffer[str] | None, encoding: str | None = None):
11501150
if hasattr(buf, "write"):
11511151
yield buf
11521152
elif isinstance(buf, str):
1153-
check_parent_directory(str(buf))
1153+
check_parent_directory(buf)
11541154
with open(buf, "w", encoding=encoding, newline="") as f:
11551155
# GH#30034 open instead of codecs.open prevents a file leak
11561156
# if we have an invalid encoding argument.

0 commit comments

Comments
 (0)