[英]PyFITS: File already exists
I'm really close to completing a large code, but the final segment of it seems to be failing and I don't know why.我真的很接近完成一个大代码,但它的最后一部分似乎失败了,我不知道为什么。 What I'm trying to do here is take an image-array, compare it to a different image array, and wherever the initial image array equals 1, I want to mask that portion out in the second image array.
我在这里尝试做的是获取一个图像数组,将其与不同的图像数组进行比较,并且无论初始图像数组等于 1,我都想在第二个图像数组中屏蔽该部分。 However, I'm getting a strange error:
但是,我收到一个奇怪的错误:
Code:代码:
maskimg='omask'+str(inimgs)[5:16]+'.fits'
newmaskimg=pf.getdata(maskimg)
oimg=pf.getdata(inimgs)
for i in range (newmaskimg.shape[0]):
for j in range (newmaskimg.shape[1]):
if newmaskimg[i,j]==1:
oimg[i,j]=0
pf.writeto('newestmask'+str(inimgs)[5:16]+'.fits',newmaskimg)
Error:错误:
/home/vidur/se_files/fetch_swarp10.py in objmask(inimgs, inwhts, thresh1, thresh2, tfdel, xceng, yceng, outdir, tmpdir)
122 if newmaskimg[i,j]==1:
123 oimg[i,j]=0
--> 124 pf.writeto('newestmask'+str(inimgs)[5:16]+'.fits',newmaskimg)
125
126
/usr/local/lib/python2.7/dist-packages/pyfits/convenience.pyc in writeto(filename, data, header, output_verify, clobber, checksum)
396 hdu = PrimaryHDU(data, header=header)
397 hdu.writeto(filename, clobber=clobber, output_verify=output_verify,
--> 398 checksum=checksum)
399
400
/usr/local/lib/python2.7/dist-packages/pyfits/hdu/base.pyc in writeto(self, name, output_verify, clobber, checksum)
348 hdulist = HDUList([self])
349 hdulist.writeto(name, output_verify, clobber=clobber,
--> 350 checksum=checksum)
351
352 def _get_raw_data(self, shape, code, offset):
/usr/local/lib/python2.7/dist-packages/pyfits/hdu/hdulist.pyc in writeto(self, fileobj, output_verify, clobber, checksum)
651 os.remove(filename)
652 else:
--> 653 raise IOError("File '%s' already exists." % filename)
654 elif (hasattr(fileobj, 'len') and fileobj.len > 0):
655 if clobber:
IOError: File 'newestmaskPHOTOf105w0.fits' already exists.
If you don't care about overwriting the existing file, pyfits.writeto
accepts a clobber
argument to automatically overwrite existing files (it will still output a warning):如果你不关心覆盖现有文件,
pyfits.writeto
接受一个clobber
参数来自动覆盖现有文件(它仍然会输出警告):
pyfits.writeto(..., clobber=True)
As an aside, let me be very emphatic that the code you posted above is very much not the right way to use Numpy.顺便说一句,让我非常强调您上面发布的代码非常不是使用 Numpy 的正确方法。 The loop in your code can be written in one line and will be orders of magnitude faster.
代码中的循环可以写在一行中,速度会快几个数量级。 For example, one of many possibilities is to write it like this:
例如,许多可能性之一是这样写:
oimg[newmaskimg == 1] = 0
Yes, add clobber = True
.是的,添加
clobber = True
。 I've used this in my codes before and it works just fine.我以前在我的代码中使用过它,它工作得很好。 Or, simply go and
sudo rm path/to/file
and get rid of them so you can run it again.或者,只需去
sudo rm path/to/file
并删除它们,以便您可以再次运行它。
I had the same issue and as it turns out using the argument clobber
still works but won't be supported in future versions of AstroPy.我遇到了同样的问题,结果证明使用参数
clobber
仍然有效,但在未来版本的 AstroPy 中将不受支持。
The argument overwrite
does the same thing and doesn't put out an error message.参数
overwrite
做同样的事情并且不会发出错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.