简体   繁体   中英

How can i tell if a given path is a directory or a file?

I need a C/C++ function like os.path.isdir(path) in Python .

I found a very similar question but i'm using Linux.

The POSIX solution is stat() :

These functions return information about a file.

Basically, you hand it an instance of the struct stat , and if the call succeeds (check this first!) you get various fields filled in that describe the file.

You can then use the S_ISDIR() macro on the st_mode field to figure out if it's a directory. I suspect this is what Python does, under the hood.

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