文件路径生成
<p>[TOC]</p>
<pre><code class="language-python">%matplotlib inline
%load_ext autoreload
%autoreload 2
import meteva.base as meb
import datetime</code></pre>
<h1>生成文件路径</h1>
<p><strong><font face="黑体" color=blue size = 5>get_path(dir,time,dt = None)</font></strong><br />
通过路径的通配模型和时间、预报时效等参数自动生成文件路径 </p>
<table>
<thead>
<tr>
<th style="text-align: left;">参数</th>
<th style="text-align: left;">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;"><strong><font face="黑体" color=blue size = 5>dir</font></strong></td>
<td style="text-align: left;">文件名的通配模式,通配模式中YYYY,YY,MM,DD,HH,FF,SS,TTT会分别对应成四位数年份,两位数年份,两位数的月、日、时,分,秒和三位数的预报时效。* 号可以通配任意长度的字符串。 如果原文件名中已经包含了MM,DD等字样,则需要在通配模式中写成M>M,D>D等形式,避免被转换成时间字符。</td>
</tr>
<tr>
<td style="text-align: left;"><strong><font face="黑体" color=blue size = 5>time</font></strong></td>
<td style="text-align: left;">起报时间</td>
</tr>
<tr>
<td style="text-align: left;"><strong>dt</strong></td>
<td style="text-align: left;">时效</td>
</tr>
<tr>
<td style="text-align: left;"><font face="黑体" color=blue size=5>return</font></td>
<td style="text-align: left;">字符串形式的路径,当采用* 号进行通配时,未匹配到相应的文件则会返回None</td>
</tr>
</tbody>
</table>
<p><strong>调用示例</strong></p>
<p>例如在H:\test_data\input\meb\文件夹下有4个文件:<br />
2020011200.000.nc<br />
2020011200.003.nc<br />
globalECMWF_IT_2020011200_VT_2020011203_FH_003_AT_003.nc<br />
globalECMWF_IT_2020011200_VT_2020011203_FH_003_DD_003.nc </p>
<pre><code class="language-python">time0 = datetime.datetime(2020,1,12,0,0)
dir = r"H:\test_data\input\meb\YYYYMMDDHH.TTT.nc"
path = meb.get_path(dir,time0)
print(path)</code></pre>
<pre><code>H:\test_data\input\meb\2020011200.000.nc</code></pre>
<pre><code class="language-python">path = meb.get_path(dir,time0,3)
print(path)</code></pre>
<pre><code>H:\test_data\input\meb\2020011200.003.nc</code></pre>
<pre><code class="language-python">dir = r"H:\test_data\input\meb\*YYYYMMDDHH*TTT*AT*"
path = meb.get_path(dir,time0,3)
print(path)</code></pre>
<pre><code>H:/test_data/input/meb/globalECMWF_IT_2020011200_VT_2020011203_FH_003_AT_003.nc</code></pre>
<pre><code class="language-python">dir = r"H:\test_data\input\meb\*YYYYMMDDHH*TTT*D>D*"
path = meb.get_path(dir,time0,3)
print(path)</code></pre>
<pre><code>H:/test_data/input/meb/globalECMWF_IT_2020011200_VT_2020011203_FH_003_DD_003.nc</code></pre>
<pre><code class="language-python">dir = r"H:\test_data\input\meb\*YYYYMMDDHH*TTT*DD*"
path = meb.get_path(dir,time0,3)
print(path)</code></pre>
<pre><code>None</code></pre>
<h1>根据文件路径创建文件夹</h1>
<p><strong><font face="黑体" color=blue size = 5>creat_path(path)</font></strong><br />
根据文件名,判断相应的文件夹是否存在,如果不存在则生成逐级文件夹 </p>
<table>
<thead>
<tr>
<th style="text-align: left;">参数</th>
<th style="text-align: left;">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;"><strong><font face="黑体" color=blue size = 5>path</font></strong></td>
<td style="text-align: left;">完整的文件路径</td>
</tr>
<tr>
<td style="text-align: left;"><font face="黑体" color=blue size=5>return</font></td>
<td style="text-align: left;">None</td>
</tr>
</tbody>
</table>
<p><strong>调用示例</strong></p>
<pre><code class="language-python">path = r"H:\test_data\output\meb\path_tools\a.txt"
meb.creat_path(path)</code></pre>
<h1>获取目录下所有文件路径</h1>
<p><strong><font face="黑体" color=blue size = 5>get_path_list_in_dir(root_dir,all_path = None)</font></strong><br />
采用递归法穷举文件夹下所有文件,包括子文件夹内的文件, 但是不包括子文件的路径 </p>
<table>
<thead>
<tr>
<th style="text-align: left;">参数</th>
<th style="text-align: left;">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;"><strong><font face="黑体" color=blue size = 5>root_dir</font></strong></td>
<td style="text-align: left;">文件夹完整路径</td>
</tr>
<tr>
<td style="text-align: left;"><font face="黑体" color=blue size=5>return</font></td>
<td style="text-align: left;">文件路径的列表</td>
</tr>
</tbody>
</table>
<p><strong>调用示例</strong></p>
<pre><code class="language-python">root_dir = r"H:\test_data\input\meb"
meb.tool.path_tools.get_path_list_in_dir(root_dir)</code></pre>
<pre><code>['H:\\test_data\\input\\meb\\2020011200.000.nc',
'H:\\test_data\\input\\meb\\2020011200.000.new.nc',
'H:\\test_data\\input\\meb\\2020011200.000.resave.nc',
'H:\\test_data\\input\\meb\\2020011200.006.nc',
'H:\\test_data\\input\\meb\\ACHN.CREF000.20200610.114200.LATLON',
'H:\\test_data\\input\\meb\\ACHN.QREF000.20191201.000000.latlon.bz2',
'H:\\test_data\\input\\meb\\B13_20200229_2100_HMW8.AWX',
'H:\\test_data\\input\\meb\\binary.grd',
'H:\\test_data\\input\\meb\\copy_test\\19120108.000',
'H:\\test_data\\input\\meb\\copy_test\\19120108.003',
'H:\\test_data\\input\\meb\\copy_test\\19120108.006',
'H:\\test_data\\input\\meb\\copy_test\\19120108.009',
'H:\\test_data\\input\\meb\\copy_test\\19120108.012',
'H:\\test_data\\input\\meb\\copy_test\\19120108.015',
'H:\\test_data\\input\\meb\\copy_test\\19120108.018',
'H:\\test_data\\input\\meb\\copy_test\\19120108.021',
'H:\\test_data\\input\\meb\\copy_test\\19120108.024',
'H:\\test_data\\input\\meb\\copy_test\\19120108.027',
'H:\\test_data\\input\\meb\\copy_test\\19120108.030',
'H:\\test_data\\input\\meb\\copy_test\\19120108.033',
'H:\\test_data\\input\\meb\\copy_test\\19120108.036',
'H:\\test_data\\input\\meb\\copy_test\\19120108.039',
'H:\\test_data\\input\\meb\\copy_test\\19120108.042',
'H:\\test_data\\input\\meb\\copy_test\\19120108.045',
'H:\\test_data\\input\\meb\\copy_test\\19120108.048',
'H:\\test_data\\input\\meb\\copy_test\\19120108.051',
'H:\\test_data\\input\\meb\\copy_test\\19120108.054',
'H:\\test_data\\input\\meb\\copy_test\\19120108.057',
'H:\\test_data\\input\\meb\\copy_test\\19120108.060',
'H:\\test_data\\input\\meb\\copy_test\\19120108.063',
'H:\\test_data\\input\\meb\\copy_test\\19120108.066',
'H:\\test_data\\input\\meb\\copy_test\\19120108.069',
'H:\\test_data\\input\\meb\\copy_test\\19120108.072',
'H:\\test_data\\input\\meb\\EC_rain20070608.024.nc',
'H:\\test_data\\input\\meb\\gds_stadata.000',
'H:\\test_data\\input\\meb\\globalECMWF_IT_2020011200_VT_2020011203_FH_003_AT_003.nc',
'H:\\test_data\\input\\meb\\globalECMWF_IT_2020011200_VT_2020011203_FH_003_DD_003.nc',
'H:\\test_data\\input\\meb\\m1.txt',
'H:\\test_data\\input\\meb\\m11.024',
'H:\\test_data\\input\\meb\\m2.024',
'H:\\test_data\\input\\meb\\m2.txt',
'H:\\test_data\\input\\meb\\m3.000',
'H:\\test_data\\input\\meb\\m3.h5',
'H:\\test_data\\input\\meb\\m3.txt',
'H:\\test_data\\input\\meb\\m4.txt',
'H:\\test_data\\input\\meb\\m8.txt',
'H:\\test_data\\input\\meb\\nc\\test.nc',
'H:\\test_data\\input\\meb\\nc\\test_isobaric.nc',
'H:\\test_data\\input\\meb\\nc\\test_surface2.nc',
'H:\\test_data\\input\\meb\\rain01.m3.txt',
'H:\\test_data\\input\\meb\\rain01.nc',
'H:\\test_data\\input\\meb\\read_stadata_from_csv_test.txt',
'H:\\test_data\\input\\meb\\read_stadata_from_csv_test2.txt',
'H:\\test_data\\input\\meb\\read_stadata_from_csv_test3.csv',
'H:\\test_data\\input\\meb\\sevp.txt',
'H:\\test_data\\input\\meb\\test.gds',
'H:\\test_data\\input\\meb\\test.grb',
'H:\\test_data\\input\\meb\\test.grb.4cc40.idx',
'H:\\test_data\\input\\meb\\test.nc',
'H:\\test_data\\input\\meb\\test_wind.gds',
'H:\\test_data\\input\\meb\\test_xy.nc',
'H:\\test_data\\input\\meb\\tp_part.nc',
'H:\\test_data\\input\\meb\\WRFPRS.GrbF03',
'H:\\test_data\\input\\meb\\WRFPRS_new.GrbF03',
'H:\\test_data\\input\\meb\\Z_SEVP_C_BFFZ_20200912080500_P_RFFC-SPCC-202009121200-16812.TXT']</code></pre>
<pre><code class="language-python"></code></pre>