简体   繁体   English

如何使用python检查[dot]文件夹是否存在

[英]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. 我正在尝试制作一个python软件包,我想从用户那里获取输入并将信息保存在根目录的.example文件夹中,以便以后可以访问该信息。 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). 我希望它能在所有操作系统(主要是Linux,MacO和Windows)上运行。

Does the isdir() function not work with hidden directories? isdir()函数不适用于隐藏目录吗? 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: 您可以使用以下命令简单地检查.example是否为目录:

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

It will give you True in case .example exists and is a directory. 如果.example存在并且是目录,它将为您提供True Otherwise, it will return False . 否则,它将返回False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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