3

我正在浏览一些文件并在我的代码中划分它们的值。for 循环用于解析文件类型的多次迭代(它更快)。但是,有时,文件可能不存在。我该如何跳过它并让 for 循环继续,因为某些文件编号可能由于某种原因不存在。

代码:

import pyfits as pf

for i in range (1,18):
   img1= pf.getdata("galmasksciPHOTOf105w"+str(i)+".fits")
   img2= pf.getdata("fittedsciPHOTOf105w"+str(i)+".fits")
   img3=(img2)[2]
   newimg=img1/img3
   pf.writeto("dividedsciPHOTOf105w"+str(i)+".fits",newimg, clobber=True)

for i in range (1,18):
   img1= pf.getdata("galmasksciPHOTOf125w"+str(i)+".fits")
   img2= pf.getdata("fittedsciPHOTOf125w"+str(i)+".fits")
   img3=(img2)[2]
   newimg=img1/img3
   pf.writeto("dividedsciPHOTOf125w"+str(i)+".fits",newimg, clobber=True)

for i in range (1,22):
   img1= pf.getdata("galmasksciPHOTOf140w"+str(i)+".fits")
   img2= pf.getdata("fittedsciPHOTOf140w"+str(i)+".fits")
   img3=(img2)[2]
   newimg=img1/img3
   pf.writeto("dividedsciPHOTOf140w"+str(i)+".fits",newimg, clobber=True)

for i in range (1,22):
   img1= pf.getdata("galmasksciPHOTOf160w"+str(i)+".fits")
   img2= pf.getdata("fittedsciPHOTOf160w"+str(i)+".fits")
   img3=(img2)[2]
   newimg=img1/img3
   pf.writeto("dividedsciPHOTOf160w"+str(i)+".fits",newimg, clobber=True)

因此,例如,fittedsciPHOTOf125w4 不存在,Python 将代码停在那里。

4

2 回答 2

7

您可以先使用os.path.existsoros.path.isfile来测试文件是否存在。或者把你的阅读陈述放在一个try...except: pass块中。

for i in range (1,18):
   if os.path.isfile("galmasksciPHOTOf105w"+str(i)+".fits") and os.path.exists("fittedsciPHOTOf105w"+str(i)+".fits"):
       img1= pf.getdata("galmasksciPHOTOf105w"+str(i)+".fits")
       img2= pf.getdata("fittedsciPHOTOf105w"+str(i)+".fits")
       img3=(img2)[2]
       newimg=img1/img3
       pf.writeto("dividedsciPHOTOf105w"+str(i)+".fits",newimg, clobber=True)

或者:

for i in range (1,18):
   try:
       img1= pf.getdata("galmasksciPHOTOf105w"+str(i)+".fits")
       img2= pf.getdata("fittedsciPHOTOf105w"+str(i)+".fits")
       img3=(img2)[2]
       newimg=img1/img3
       pf.writeto("dividedsciPHOTOf105w"+str(i)+".fits",newimg, clobber=True)
   except IOError:
       pass
于 2013-07-24T04:13:47.013 回答
2

我想把它写成评论,但可惜,这在这种程度上不起作用。

您应该尽量避免代码重复:

import pyfits as pf

def process_file(filenamebase)
    img1 = pf.getdata("galmask" + filenamebase)
    img2 = pf.getdata("fitted" + filenamebase)
    img3 = img2[2]
    newimg = img1/img3
    pf.writeto("divided" + filenamebase, newimg, clobber=True)

for i in range (1,18):
    process_file("PHOTOf105w" + str(i) + ".fits")
    # or
    process_file("PHOTOf105w%d.fits" % i)
    # or
    process_file("PHOTOf105w{0}.fits".format(i))

for i in range (1,18):
    process_file("PHOTOf125w{0}.fits".format(i))

for i in range (1,22):
    process_file("PHOTOf140w{0}.fits".format(i))

for i in range (1,22):
    process_file("PHOTOf160w{0}.fits".format(i))

现在你可以应用zhangyangyu的方法

def process_file(filenamebase)
    import sys # for stderr
    try:
        img1 = pf.getdata("galmask" + filenamebase)
        img2 = pf.getdata("fitted" + filenamebase)
        img3 = img2[2]
        newimg = img1/img3
        pf.writeto("divided" + filenamebase, newimg, clobber=True)
    except IOError, e:
        print >>sys.stderr, e # in order to know what happened.

并且您同时在所有 4 个循环中进行了这一更改。

于 2013-07-24T07:07:57.393 回答