简体   繁体   中英

python not able to read a file that exists

I wrote the following script

#!/usr/bin/env python 
import numpy as np 
import matplotlib.pyplot as plt 

########
N = 92 
lookup  = 'The forecast spread is'
    iday_start    = 1 
    iday_end      = 1
    year        = 2013
    month       = 07  
    extension_type1  = '-RTPSinfl.dat'
    extension_type2  = 'engl_var_anal.'
    extension        = 2
######### Append the files into files
    for iday in range (iday_start, iday_end+1):
        day =  str(0) + str(iday) 
        for itime in range(0,24,6) :   
           if itime < 12: 
           ihour = str(0) + str(itime)
        else:
           ihour =  str(itime) 
        if extension == 1 :  
          file  = str(year)+str(month) + day + ihour + extension_type1
          print(file)  
        elif  extension == 2 :
          file = extension_type2 + str(year)+str(month)+day+ihour+'.1.out'
          print(file)         
#========          
      f = open(file)
      lines = f.readlines()
      f.close() 
      with open(file) as myFile:
          for num, line in enumerate(myFile,1):
              if lookup in line:
                  print 'found at line:', num  
          num = num+2
          numN = num + N
          lrange = range(num,numN)
          for l in lrange:
                for ii in range(0,7): 
                    nstart = numN + ii * (N+2) + 1
                    lrange = range(nstart,nstart+N)
                    for l in lrange:
                        print lines[l],                 
      myFile.close()

and here is the error message

IOError: [Errno 2] No such file or directory: 'engl_var_anal.201370100.1.out'

The file engl_var_anal.201370100.1.out does exists.

Python will load the file from the directory you are running the script from, NOT from the location of the script. If this doesn't help could you maybe give us the exact command you are running and the full path of where that file is?

Edit :

One thing to look for is that you have the exact same number of digits in the filename as your program is outputting. This is a common mistake when dealing with printing digits to strings. It looks like you expect your month to be 2 digits "07", but your output filename only has 1. Double check this.

If you need 2 digits do some kind of string formatting like:

file = extension_type2 + "%04d%02d%02d%04d" % (year, month, day, ihour) + ".1.out"

Or use datetime objects and strftime to format a date/time.

If you cd /dir/with/files_and_script , then run ls -l (assuming linux shell) and you see "test.py" and "engl_var_anal.201370100.1.out" and then run python , ...some code..., execfile("test.py") ...this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM