使用os.path.basename

时间:2015-11-06 16:03:35

标签: python netcdf os.path

我的Python代码有问题,没有创建我需要的输出netcdf文件,代码太长但基本的想法是:

 module import part
 vrs = !ls -d /home/files/out
 vrspatterns = [vr + 'FILE_2015????' for vr in vrs]
 vrsoutpaths = ['finalfile_vr' + os.path.basename(vr) + 2012_ftests.nc for vr in vrs]
 qrs = !ls -d /home/files/out/second
 qrspatterns = [qr + 'FILE_2015_2????' for qr in qrs]
 qrsoutpaths = ['finalfile_qr' + os.path.basename(qr) + 2012_ftests2.nc for qr in qrs]
 inpatterns = vrspatterns + qrspatterns
 outpaths = vrsoutpaths + qrsoutpaths

更多不会崩溃的编码

 for inpattern, outpath in zip(inpattern,outpaths)
      cmd= 'ncgen ...'
      f = Dataset(outpath, 'a')

因此,当它开始运行时,错误是:

 netCDF4.pyx in netCDF4.Dataset.__init__ (netCDF4.c:22731)()

 RuntimeError: No such file or directory

并且它在f = Dataset(outpath,'a')行崩溃。我无法找到使其无效的outpath有什么问题,因此无法在cmd生成文件。

谢谢。

1 个答案:

答案 0 :(得分:0)

您正在追加模式a打开文件,在您的解释中,您说您正在尝试创建该文件。重现此行为会引发您引用的相同错误:

In [1]: import netCDF4

In [2]: netCDF4.Dataset('test.nc', 'a')
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-aa44b76f4b7f> in <module>()
----> 1 netCDF4.Dataset('test.nc', 'a')

netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Dataset.__init__ (netCDF4/_netCDF4.c:9689)()

RuntimeError: No such file or directory

尝试以写入模式netCDF4.Dataset(outpath, 'w')打开文件。另外,@ david-cullen建议在打开文件之前打印outpath是一个很好的建议。