小编Wil*_*son的帖子

Python检查目录是否存在,然后在必要时创建它并将图形保存到新目录?

所以我希望它独立于使用代码的计算机,所以我希望能够在当前目录中创建一个目录并将我的绘图保存到该新文件中.我看了一些其他的问题并试了一下(我有两次尝试,一次注释掉):

    import os
    from os import path
    #trying to make shift_graphs directory if it does not already exist:

    if not os.path.exists('shift_graphs')
        os.mkdirs('shift_graphs')

    plt.title('Shift by position on '+str(detector_num)+'-Detector')
    #saving figure to shift_graphs directory
    plt.savefig(os.path.join('shift_graphs','shift by position on '+str(detector_num)+'-detector'))
    print "plot 5 done"
    plt.clf
Run Code Online (Sandbox Code Playgroud)

我收到错误:

AttributeError: 'module' object has no attribute 'mkdirs'
Run Code Online (Sandbox Code Playgroud)

我也想知道我在目录中保存它的想法是否有效,我无法测试,因为我在上面的部分中遇到了错误.

python

7
推荐指数
2
解决办法
4万
查看次数

标签 统计

python ×1