首页 > 解决方案 > 如何在python中作为参数传递时检查目录是否存在?

问题描述

我正在尝试将目录作为参数传递并检查它的存在 -

def validatePath(DirectoryName):
    pathtodata="/temp/project/data/"
    if os.path.exists('pathtodata'DirectoryName):
       return True
    return False

parser = argparse.ArgumentParser()
parser.add_argument("DirectoryName", nargs='?', help="Input folder Name", type=str)
args = parser.parse_args()
myDir = args.DirectoryName
validatePath(myDir)

错误: os.path.exists('pathtodata'DirectoryName) 行中的语法错误:

标签: python

解决方案


在 Python 中,组合路径的方式'pathtodata'DirectoryName不是os.path.join(pathtodata, DirectoryName).


推荐阅读