9

I try to get the name of subdirectories with Python3 script on Windows10. Thus, I wrote code as follows:

from pathlib2 import Path
p = "./path/to/target/dir"
[str(item) for item in Path(p).rglob(".")]
# obtained only subdirectories path names including target directory itself.

It is good for me to get this result, but I don't know why the pattern of rglob argument returns this reuslt.

Can someone explain this?

Thanks.

tsekine
  • 103
  • 1
  • 1
  • 5
  • 2
    Do you know what "glob" means, or what exactly is confusing about this result? [The python docs](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rglob) explain the difference between `glob` and `rglob`, and [wikipedia](https://en.wikipedia.org/wiki/Glob_(programming)) explains glob patterns in general. – Arne Aug 31 '18 at 07:59
  • 1
    Yes, I mean the latter of your comment, confused about this result. Though I usually use this method with wildcard, in this case, this pattern, in shortly, without wild and only one dot shows only directory path objects. What does the one-dot pattern (“.”) mean for glob? – tsekine Aug 31 '18 at 08:15
  • Ah, I think I know what you mean. I'll try to write an answer. – Arne Aug 31 '18 at 08:28

1 Answers1

5

Every directory in a posix-style filesystem features two files from the get go: .., which refers to the parent directory, and ., which refers to the current directory:

$ mkdir tmp; cd tmp
tmp$ ls -a
. ..
tmp$ cd .
tmp$  # <-- still in the same directory

- with the notable exception of /.., which refers to the root itself since the root has not parent.

A Path object from python's pathlib is, when it is created, just a wrapper around a string that is assumed to point somewhere into the filesystem. It will only refer to something tangible when it is resolved:

>>> Path('.')
PosixPath('.')  # just a fancy string
>>> Path('.').resolve()
PosixPath('/current/working/dir')  # an actual point in your filesystem

The bottom line is that

  • the paths /current/working/dir and /current/working/dir/. are, from the filesystem's point of view, completely equivalent, and
  • a pathlib.Path will also reflect that as soon as it is resolved.

By matching the glob call to ., you found all links pointing to the current directories below the initial directory. The results from glob get resolved on return, so the . doesn't appear in there any more.

As a source for this behavior, see this section of PEP428 (which serves as the specification for pathlib), where it briefly mentions path equivalence.

Arne
  • 17,706
  • 5
  • 83
  • 99
  • Thank you for your kind answer. I know `ls -a` command shows one and two dots, of course other files or directories, but I've never thought that much. Thank you for your time! – tsekine Sep 03 '18 at 04:26