Python命令等待shell(ubuntu csh)完成操作/返回码
问题描述:
我正在运行一个Python命令+ API来访问ECMWF(欧洲中程天气预报中心)数据服务器(称为MARS)并下载一些文件(天气数据)。我启动与外壳的Python代码(我使用csh)的壳做./python_script.py
Python命令等待shell(ubuntu csh)完成操作/返回码
,如果我在脚本下载一个文件(1999一年range(1999, 2000)
运行正常,而不是range(1998, 2000)
在下面的例子失败Python脚本)。现在我想下载它们中的很多,并因此循环多年。
我的问题是,似乎Python脚本不会等待shell命令/ API完成并在下一年继续。它导致错误。该文件生成但零大小。
我想知道是否可以指定Python脚本在继续执行下一个for/loop步骤之前等待在shell窗口中查找某些关键字。
我知道我在这种情况下使用了一些特定的API,可能会找到另一个特定于API的解决方案来这样做,但在shell中识别某些打印输出似乎更容易。
这可能是例如“传输速度”,这似乎显示在shell窗口只有当工作已经完成,查看日志(最后一次成功行)我从壳保存(与./python_script.py >& log_file.log
。
我的Python代码是:
#!/usr/bin/env python
for year in range(1998, 2000):
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
"class": "e2",
"dataset": "era20c",
"date": '%d-07-01/%d-07-02' % (year,year),
"domain": "g",
"area" : "12/-72/-67/22",
"grid" : "1.0/1.0",
"expver": "1",
"param": "214.140/233.140",
"step": "3/9/15/21",
"format" : "netcdf",
"stream": "wave",
"target": '/home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/%d/test_era20c_wave_set1.nc' % (year),
"time": "06",
"type": "fc",
})
我的日志的最后几行的一个文件的仅下载(成功):
2016-02-13 16:00:21 Request is complete
2016-02-13 16:00:21 Transfering 239.441 Kbytes into /home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/1999/test_era20c_wave_set1.nc
2016-02-13 16:00:21 From http://stream.ecmwf.int/data/atls04/data/data01/scratch/_grib2netcdf-atls04-95e2cf679cd58ee9b4db4dd119a05a8d-JLUk0w.nc
2016-02-13 16:00:28 Transfer rate 32.6278 Kbytes/s
答
没有必要在循环导入,也它MIG ht是indentaiton问题。如果您的脚本与您提供的脚本相同,则retrieve
不在循环中。 Python中的缩进很重要。
尝试重写剧本是这样的:
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
for year in range(1998, 2000):
server.retrieve({
"class": "e2",
"dataset": "era20c",
"date": '%d-07-01/%d-07-02' % (year,year),
"domain": "g",
"area" : "12/-72/-67/22",
"grid" : "1.0/1.0",
"expver": "1",
"param": "214.140/233.140",
"step": "3/9/15/21",
"format" : "netcdf",
"stream": "wave",
"target": '/home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/%d/test_era20c_wave_set1.nc' % (year),
"time": "06",
"type": "fc",
})
确实有数据的所有年份?如果您尝试使用'range(1998,1999)',它会起作用吗? –
嗨阿特,感谢您的快速回答,是的,有数据,它的工作发现与范围(1998,1999) – Nicolas