classification
Title: pathlib .glob('*/') returns files as well as directories
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder: pathlib glob ignores trailing slash in pattern
View: 22276
Assigned To: emilyemorehouse Nosy List: brianmsheldon, emilyemorehouse, robbuckley
Priority: normal Keywords:

Created on 2018-04-30 13:17 by robbuckley, last changed 2018-11-17 20:25 by E Kawashima. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 10349 E Kawashima, 2018-11-17 20:25
Messages (5)
msg315949 - (view) Author: (robbuckley) Date: 2018-04-30 13:17
Path.cwd().glob('/*') seems to yield all files and folders in cwd, the same as .glob('*').

I believe that glob('*/') should yield only directories, and glob('*') all files and directories. this behaviour isnt documented one way or the other. But it would be consistent with glob.glob()

console dump follows:

Python 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:14:23) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import os

In [2]: from pathlib import Path

In [6]: import glob

In [13]: list(p.glob('*/'))
Out[13]: 
[PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d1'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d2')]

In [14]: list(p.glob('*'))
Out[14]: 
[PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d1'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt'),
 PosixPath('/Users/robertbuckley/python-scripts/pathlib_bug/d2')]

In [15]: glob.glob(os.path.join(str(p), '*/'))
Out[15]: 
['/Users/robertbuckley/python-scripts/pathlib_bug/d1/',
 '/Users/robertbuckley/python-scripts/pathlib_bug/d2/']

In [16]: glob.glob(os.path.join(str(p), '*'))
Out[16]: 
['/Users/robertbuckley/python-scripts/pathlib_bug/f1.txt',
 '/Users/robertbuckley/python-scripts/pathlib_bug/f3.txt',
 '/Users/robertbuckley/python-scripts/pathlib_bug/d1',
 '/Users/robertbuckley/python-scripts/pathlib_bug/f2.txt',
 '/Users/robertbuckley/python-scripts/pathlib_bug/d2']
msg315950 - (view) Author: (robbuckley) Date: 2018-04-30 13:23
this is workaroundable via constructions like:

list(x for x in p.glob('*/') if x.is_dir())

but I think there's value in behaving like glob.glob(), and keystroke avoidance for what must be a fairly common use case
msg316007 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2018-05-01 17:35
Good find -- I agree that when using Path.cwd().glob('*/'), it should only return directories. This follows the original glob library's functionality as well as the Unix expectation (I believe Windows as well, but I'll double check).

I'll work on a fix!
msg316009 - (view) Author: (robbuckley) Date: 2018-05-01 18:51
I checked this on Mac and Windows, pathlib and the old glob std lib behave
the same on both in this respect

On Tue, 1 May 2018, 18:35 Emily Morehouse, <report@bugs.python.org> wrote:

>
> Emily Morehouse <emily@cuttlesoft.com> added the comment:
>
> Good find -- I agree that when using Path.cwd().glob('*/'), it should only
> return directories. This follows the original glob library's functionality
> as well as the Unix expectation (I believe Windows as well, but I'll double
> check).
>
> I'll work on a fix!
>
> ----------
> assignee:  -> emilyemorehouse
> nosy: +emilyemorehouse
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue33392>
> _______________________________________
>
msg316192 - (view) Author: Brian Sheldon (brianmsheldon) Date: 2018-05-05 02:46
This appears to be a duplicate of #22276
History
Date User Action Args
2018-11-17 20:25:43E Kawashimasetpull_requests: + pull_request9832
2018-05-05 04:14:00SilentGhostsetstatus: open -> closed
superseder: pathlib glob ignores trailing slash in pattern
resolution: duplicate
stage: resolved
2018-05-05 02:46:23brianmsheldonsetnosy: + brianmsheldon
messages: + msg316192
2018-05-01 18:51:30robbuckleysetmessages: + msg316009
2018-05-01 17:35:10emilyemorehousesetassignee: emilyemorehouse

messages: + msg316007
nosy: + emilyemorehouse
2018-04-30 13:23:20robbuckleysetmessages: + msg315950
2018-04-30 13:17:33robbuckleycreate