python - 如何在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 中,组合路径的方式'pathtodata'DirectoryName
不是os.path.join(pathtodata, DirectoryName)
.
推荐阅读
- c# - C# 从列表和输入框中选择并传递参数
- javascript - 如何在 OpenLayers 5 中删除绘图?
- sql - 按周的星期一查询数据库和分组
- reactjs - React:将整数转换为带点分隔符的数字
- android - 得到“测试APK不包含用户指定的测试运行器类或清单文件。” 使用 Firebase
- java - javax.xml.stream.XMLStreamException: ParseError at [row,col]:[X,X] - JavaFX
- javascript - 如何读取sharp返回的图像的Buffer数据?
- python - 使用 pyinstaller 打包 .txt 文件以在 python 可执行文件中使用
- typescript - 新的 TypeScript 代码不会生成新的 JavaScript 代码
- angular - 路由器插座在错误位置意外激活