我正在开发一个涉及Python Flask API的Python Web应用程序。我在Windows上进行开发,并使用Postman测试了所有API。一切正常。在我的Web应用程序项目中,如果某些目录不存在,我必须创建它们,因此我使用以下代码:
if not os.path.isdir("dataset/" + client_name):
# if client name directory is not created, then create it
client_dir = curr_path + '\\' + 'dataset\\' + client_name
os.mkdir(client_dir)
我正在将这个web应用程序部署在pythonanywhere.com上。Pythonanywhere使用Linux平台,而我使用Windows进行开发,因此会出现一些问题。在Windows中,我们使用
\
表示目录,但在Linux中,它使用/
。我该如何在Windows上进行开发并在Linux上部署?是否可以定义某种配置来处理这个问题?
谢谢!
os.path.join(...)
来创建路径将会适当地格式化给定系统的路径:https://docs.python.org/3.7/library/os.path.html#os.path.join - SyntaxVoidos.path.sep
。如果你从pathlib import Path
,那么client_dir = Path.cwd() / 'dataset' / client_name
。pathlib
使用/
作为连接运算符,但它根据你所在的平台正确地解释它。然后client_dir.mkdir()
。 - Steven Rumbalski