我想保存更改名稱爲不同的文件夾的腳本文件是:設置numpy.savetxt路徑 - 文件名包含循環變量
save_field('./cycle_1_580-846/txt/"frame_" + str(580 + j) + "_to_" + str(581 + j) + ".txt"', "Data68_0" + str(580 + j) + ".tif", str(580 + j) + "_to_" + str(581 + j), scale = 1500, width = 0.0025)
現在保存的文件名與循環變量我跟着this post。
我天真地以爲用「和」將解決這個問題,但是,如果我這樣做,我得到的右文件夾,但有錯誤名的文件(在這種情況下:‘frame_’+ str(580 + j)+「to」+ str(581 + j)+「.txt)(我想要:frame_580_to_581.txt)。如果我沒有設定路徑,我沒有問題。
有沒有一種巧妙的方法可以解決這個問題?
乾杯!
編輯 j是隻是在一定範圍內的文件(在這種情況下,從0到270,遞增1)
也許這也將有助於
def save_field(filename, background, new_file, **kw):
""" Saves quiver plot of the data stored in the file
Parameters
----------
filename : string
the absolute path of the text file
background : string
the absolute path of the background image file
new_file : string
the name and format of the new file (png preferred)
Key arguments : (additional parameters, optional)
*scale*: [None | float]
*width*: [None | float]
"""
a = np.loadtxt(filename)
pl.figure()
bg = mpimg.imread(background)
imgplot = pl.imshow(bg, origin = 'lower', cmap = cmps.gray)
pl.hold(True)
invalid = a[:,3].astype('bool')
valid = ~invalid
pl.quiver(a[invalid,0],a[invalid,1],a[invalid,2],a[invalid,3],color='r',**kw)
pl.quiver(a[valid,0],a[valid,1],a[valid,2],a[valid,3],color='r',**kw)
v = [0, 256, 0, 126]
axis(v)
pl.draw()
pl.savefig(new_file, bbox_inches='tight')
謝謝你的深入答案,我會給你一個建議,讓你知道它是如何工作的! 爲「和'用法輸入乾杯,我認爲,因爲我有我所有的」「和」關閉它不會是一個問題! –
工作就像一個魅力!乾杯! –
非常好。很高興我能幫助!祝你好運一切。 –