Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pathlib's Path.mkdir() should allow for "mkdir -p" functionality #65738

Closed
garrison mannequin opened this issue May 20, 2014 · 8 comments
Closed

pathlib's Path.mkdir() should allow for "mkdir -p" functionality #65738

garrison mannequin opened this issue May 20, 2014 · 8 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@garrison
Copy link
Mannequin

garrison mannequin commented May 20, 2014

BPO 21539
Nosy @warsaw, @pitrou, @berkerpeksag
Files
  • issue21539.diff
  • issue21539_v2.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/warsaw'
    closed_at = <Date 2014-08-05.15:29:26.112>
    created_at = <Date 2014-05-20.03:53:56.770>
    labels = ['type-feature', 'library']
    title = 'pathlib\'s Path.mkdir() should allow for "mkdir -p" functionality'
    updated_at = <Date 2014-08-05.18:29:38.923>
    user = 'https://bugs.python.org/garrison'

    bugs.python.org fields:

    activity = <Date 2014-08-05.18:29:38.923>
    actor = 'berker.peksag'
    assignee = 'barry'
    closed = True
    closed_date = <Date 2014-08-05.15:29:26.112>
    closer = 'barry'
    components = ['Library (Lib)']
    creation = <Date 2014-05-20.03:53:56.770>
    creator = 'garrison'
    dependencies = []
    files = ['35303', '35421']
    hgrepos = []
    issue_num = 21539
    keywords = ['patch']
    message_count = 8.0
    messages = ['218832', '218842', '219233', '219238', '219239', '219468', '224839', '224842']
    nosy_count = 5.0
    nosy_names = ['barry', 'pitrou', 'python-dev', 'berker.peksag', 'garrison']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue21539'
    versions = ['Python 3.5']

    @garrison
    Copy link
    Mannequin Author

    garrison mannequin commented May 20, 2014

    As of Python 3.2, os.makedirs() takes exist_ok as a keyword argument. If set to true, the function does not raise an error if the directory already exists. This makes the function's behavior similar to mkdir -p on the UNIX commandline. (See https://docs.python.org/3/library/os.html#os.makedirs)

    However, this functionality is missing in Python's new pathlib. In fact, the documentation for Path.mkdir() even mentions that if parent=True is passed, then it should behave like mkdir -p. This is accurate in that it indeed recursively makes any parent directories necessary, but it is inaccurate in that it raises an error if the directory already exists.

    I propose that either parents=True should imply that there is no error if the directory already exists (which would be a backwards-incompatible change); or, there could be a new exist_ok=True, which could suppress the error for an existing directory. Either way, it ought to be possible to get the mkdir -p functionality easily from pathlib. And the documentation for Path.mkdir() should be updated to explain how to get this mkdir -p functionality, with the existing (inaccurate) mkdir -p mention removed.

    @garrison garrison mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 20, 2014
    @pitrou
    Copy link
    Member

    pitrou commented May 20, 2014

    I think we can a new exist_ok parameter indeed. It should probably be False by default, though.

    @warsaw
    Copy link
    Member

    warsaw commented May 27, 2014

    I've just been writing some new code to use pathlib and ran into this one myself. An exist_ok=False is fine, although it's a slight shame that for backward compatibility we can't adopt os.makedirs() signature exactly.

    @pitrou
    Copy link
    Member

    pitrou commented May 27, 2014

    Le 27/05/2014 20:32, Barry A. Warsaw a écrit :

    I've just been writing some new code to use pathlib and ran into
    this
    one myself. An exist_ok=False is fine, although it's a slight shame that
    for backward compatibility we can't adopt os.makedirs() signature exactly.

    What do you mean by that? The os.makedirs() signature is
    os.makedirs(name, mode=0o777, exist_ok=False)

    @warsaw
    Copy link
    Member

    warsaw commented May 27, 2014

    On May 27, 2014, at 08:48 PM, Antoine Pitrou wrote:

    What do you mean by that? The os.makedirs() signature is
    os.makedirs(name, mode=0o777, exist_ok=False)

    Right, but this is Path.mkdir's signature:

    Path.mkdir(mode=0o777, parents=False)

    so it's too late to make exist_ok=False the second argument (i.e. the one
    after mode). Oh well.

    @berkerpeksag
    Copy link
    Member

    Updated patch. Thank you Barry and Antoine.

    @warsaw
    Copy link
    Member

    warsaw commented Aug 5, 2014

    I like it. Thanks for the contribution! Assigning to myself since I plan on working to land this in 3.5.

    @warsaw warsaw self-assigned this Aug 5, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 5, 2014

    New changeset 8af80d68bcdc by Barry Warsaw in branch 'default':

    @warsaw warsaw closed this as completed Aug 5, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants