【发布时间】:2015-09-09 14:17:38
【问题描述】:
所以我希望它独立于使用代码的计算机,所以我希望能够在当前目录中创建一个目录并将我的图保存到那个新文件中。我查看了其他一些问题并尝试了这个(我有两次尝试,一次注释掉了):
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
我得到错误:
AttributeError: 'module' object has no attribute 'mkdirs'
我还想知道我将其保存在目录中的想法是否可行,由于我在上述部分中遇到的错误,我无法对其进行测试。
【问题讨论】:
-
有
os.mkdir和os.makedirs。没有os.mkdirs。 (正如已经向您解释过的错误消息。) -
好的,抱歉,我对编程很陌生
-
我用
if not os.path.exists(new_path):\os.makedirs(new_path)
标签: python