在Windows中进行开发和在Linux Python API中进行测试时如何管理目录路径

S A*_*rew 3 python

我正在开发涉及少量python flask api的python web应用程序。我正在Windows上开发此程序,并已使用邮递员测试了所有api。一切正常。在我的webapp项目中,如果目录不存在,我必须创建几个目录,为此,我使用以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

我正在pythonanywhere.com上部署此webapp 。这使用linux作为平台,由于出现问题,我正在使用Windows进行开发。现在在Windows中,我们使用\目录,但在Linux中,使用/

在Windows上工作并在Linux上部署时,我该如何管理它。我可以定义某种配置吗?

谢谢

Syn*_*ica 5

您可以避免在代码中一起使用斜杠。使用构建路径os.path.join。在您发布的示例中,您要做的就是更改

client_dir = curr_path + '\\' + 'dataset\\' + client_name
Run Code Online (Sandbox Code Playgroud)

client_dir = os.path.join(curr_path, "dataset", client_name)
Run Code Online (Sandbox Code Playgroud)

编辑:您还应该更改

if not os.path.isdir("dataset/" + client_name):
Run Code Online (Sandbox Code Playgroud)

if not os.path.isdir(os.path.join("dataset", client_name))
Run Code Online (Sandbox Code Playgroud)

并且将针对运行代码的任何系统适当地构建路径。