简体   繁体   中英

How to check if a PosixPath is a directory or a file

With Path I can run path.is_dir() , how can I do the same thing with PosixPath ?

How can I do the same thing with PosixPath ?

Exactly the same way. PosixPath extends Path so has all the underlying methods:

Python 3.9.0 (default, Oct 12 2020, 02:44:01)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import pathlib, os

>>> pathlib.PosixPath("/").is_dir()
True

>>> os.system("touch /tmp/aFile.txt")

>>> pathlib.PosixPath("/tmp/aFile.txt").is_dir()
False

>>> pathlib.PosixPath("/tmp/aFile.txt").is_file()
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM