学习笔记 | Python和linux分别怎么遍历子文件夹下的文件

2024-06-20 19:11:48 浏览数 (2)

前言

当我们进行批量处理时经常会碰到想要找子文件夹下的特定文件而不得不一个个文件夹翻找的情况

那么我们学会使用glob的匹配方便就能快速得到想要的文件列表

下面开始实验吧

假定我们要找批量以gz为后缀的文件 这要求我们对文件结构比较熟悉,我们指定在input下的第四层子文件夹下的gz文件

In [2]:

代码语言:javascript复制
代码语言:javascript复制
import glob
wenjian = glob.glob('/home/mw/input/*/*/*/*/*.gz')
wenjian
代码语言:javascript复制

Out[2]:

代码语言:javascript复制
['/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/furuno/2006_20220324_000000_000.scnx.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/furuno/0080_20210730_160000_01_02.scn.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021735-fbg---bin.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-200608281420-fbg---bin.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021745-fbg---bin.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021655-fbg---bin.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/sample.dx.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021740-fbg---bin.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.BAS71P2.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.PPI71P4.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.SRI71PF.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703221033.RHI71PE.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.CAP71P6.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.TOP71NY.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.THK71P3.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.MAX71NP.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220553.SHE71PH.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.RTI71PG.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.HMX71P9.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/bin_coords_tur.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/annual_rainfall_fbg.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_dBZ_fbg.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_dBZ_tur.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/target_coords.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/msf_xband.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_R_tur.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_R_fbg.gz',
 '/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/bin_coords_fbg.gz']

这时候有小朋友问,如果我对文件结构不熟咋办

那么我们对文件结构不那么熟悉时,可以使用linux的find语句

代码语言:javascript复制
代码语言:javascript复制
!find /home/mw/input/* -type f -name "*.gz"
代码语言:javascript复制
代码语言:javascript复制
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-pg_10000-1408102045-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ru_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rw_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-w1_10000-1408110550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-eb_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rz_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ew_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rb_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-sf_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-w4_10000-1408110550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ry_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ez_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-eh_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-sq_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rx_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ex_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-w3_10000-1408110550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rl_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-sh_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-rh_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-w2_10000-1408110550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-wx_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/showcase/raa01-ey_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/asc/RW-20221018.tar.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_000.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_095.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_010.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RQ2210180700_120.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_115.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_060.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_100.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_120.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_105.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_015.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_020.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_075.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_025.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_030.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_005.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_045.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_070.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_110.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_080.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_050.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RQ2210180700_000.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_090.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_085.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RQ2210180700_060.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_035.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_065.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_040.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/radvor/RE2210180700_055.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-sf_10000-1305280050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-rw_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-sf_10000-1305270050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-%j_10000-2108010550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-sf_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-sf_10000-1406100050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-rx_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-%m_10000-2108010550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-rw_10000-1408030950-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-ex_10000-1408102050-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa01-%y_10000-2108010550-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/radolan/misc/raa00-pc_10015-1408030905-dwd---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/furuno/2006_20220324_000000_000.scnx.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/furuno/0080_20210730_160000_01_02.scn.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021735-fbg---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-200608281420-fbg---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021745-fbg---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021655-fbg---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/sample.dx.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/dx/raa00-dx_10908-0806021740-fbg---bin.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.BAS71P2.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.PPI71P4.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.SRI71PF.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703221033.RHI71PE.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.CAP71P6.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.TOP71NY.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.THK71P3.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.MAX71NP.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220553.SHE71PH.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.RTI71PG.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/sigmet/SUR160703220000.HMX71P9.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/bin_coords_tur.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/annual_rainfall_fbg.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_dBZ_fbg.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_dBZ_tur.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/target_coords.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/msf_xband.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_R_tur.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/polar_R_fbg.gz
/home/mw/input/wrablib9842/wradlib-data-main/wradlib-data-main/misc/bin_coords_fbg.gz

find 用于搜索指定目录层次结构中的文件。 /home/mw/input/ 指定 find 查找文件的目录(及其子目录) -type f 将搜索限制为仅文件,不包括目录。 -name 指定匹配的文件名

插播寻物启事

师兄于太古里丢失鹦鹉一只,请见到者拨号114514。另外,师兄不同意鹦鹉对课题组的看法。

0 人点赞