简体   繁体   中英

How to check if [dot]folders exist using python

I'm trying to make a python package and I want to take input from the user and save the info in a .example folder in the root directory so that I can access that info later. This is what I implemented but it's not working -

def root_path():
    return os.path.abspath(os.sep)

if os.path.isdir(os.path.join(root, ".example")):
    #get info
else:
    #create .example in root

I want this to work on all operating system (Linux, MacOs and Windows majorly).

Does the isdir() function not work with hidden directories? What is the right way to do this?

To check if a path exists or not, you can do this

>>> import os
>>> os.path.exists(".hidden-folder")
True

You can simply check if .example is a directory or not by using:

>>> import os.path
>>> os.path.isdir('.example') 

It will give you True in case .example exists and is a directory. Otherwise, it will return False .

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