4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
热卖商品
新闻详情
如何使用glob递归查找文件翻译_落雪snowflake的博客-CSDN博客
来自 : CSDN技术社区 发布时间:2021-03-25
path /span span style color:#303336 /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 normpath /span span style color:#303336 ( /span span style color:#303336 sys /span span style color:#303336 . /span span style color:#303336 argv /span span style color:#303336 [ /span span style color:#7d2727 1 /span span style color:#303336 ]) /span span style color:#101094 print /span span style color:#303336 ( /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 basename /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 )) /span span style color:#303336 dirlist /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 ) /span /code /span /span

分享改善这个答案

于2013年7月27日18:12 回答

\"\"

Shaurya Gupta

154126

添加评论

2

 

那个使用fnmatch或正则表达式

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 fnmatch /span span style color:#303336 , /span span style color:#303336 os /span span style color:#101094 def /span span style color:#303336 filepaths /span span style color:#303336 ( /span span style color:#303336 directory /span span style color:#303336 , /span span style color:#303336 pattern /span span style color:#303336 ): /span  span style color:#101094 for /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 dirs /span span style color:#303336 , /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#303336 directory /span span style color:#303336 ): /span  span style color:#101094 for /span span style color:#303336 basename /span span style color:#101094 in /span span style color:#303336 files /span span style color:#303336 : /span  span style color:#101094 try /span span style color:#303336 : /span span style color:#303336  matched /span span style color:#303336 /span span style color:#303336 pattern /span span style color:#303336 . /span span style color:#303336 match /span span style color:#303336 ( /span span style color:#303336 basename /span span style color:#303336 ) /span  span style color:#101094 except /span span style color:#2b91af AttributeError /span span style color:#303336 : /span span style color:#303336  matched /span span style color:#303336 /span span style color:#303336 fnmatch /span span style color:#303336 . /span span style color:#303336 fnmatch /span span style color:#303336 ( /span span style color:#303336 basename /span span style color:#303336 , /span span style color:#303336 pattern /span span style color:#303336 ) /span  span style color:#101094 if /span span style color:#303336 matched /span span style color:#303336 : /span  span style color:#101094 yield /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 basename /span span style color:#303336 ) /span  span style color:#858c93 # usage /span  span style color:#101094 if /span span style color:#303336 __name__ /span span style color:#303336 /span span style color:#7d2727 __main__ /span span style color:#303336 : /span  span style color:#101094 from /span span style color:#303336 pprint /span span style color:#101094 import /span span style color:#303336 pprint /span span style color:#101094 as /span span style color:#303336 pp /span span style color:#101094 import /span span style color:#303336 re path /span span style color:#303336 /span span style color:#303336 r /span span style color:#7d2727 /Users/hipertracker/app/myapp /span span style color:#303336  pp /span span style color:#303336 ([ /span span style color:#303336 x /span span style color:#101094 for /span span style color:#303336 x /span span style color:#101094 in /span span style color:#303336 filepaths /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 , /span span style color:#303336 re /span span style color:#303336 . /span span style color:#303336 compile /span span style color:#303336 ( /span span style color:#303336 r /span span style color:#7d2727 .*\\.py$ /span span style color:#303336 ))]) /span span style color:#303336  pp /span span style color:#303336 ([ /span span style color:#303336 x /span span style color:#101094 for /span span style color:#303336 x /span span style color:#101094 in /span span style color:#303336 filepaths /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 , /span span style color:#7d2727 *.py /span span style color:#303336 )]) /span /code /span /span 

分享改善这个答案

于2013年8月2日16:01 回答

\"\"

hipertracker

21382212

添加评论

2

 

除了建议的答案 你可以用一些懒惰的生成和列表理解魔法来做到这一点

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 os /span span style color:#303336 , /span span style color:#303336 glob /span span style color:#303336 , /span span style color:#303336 itertoolsresults /span span style color:#303336 /span span style color:#303336 itertools /span span style color:#303336 . /span span style color:#303336 chain /span span style color:#303336 . /span span style color:#303336 from_iterable /span span style color:#303336 ( /span span style color:#303336 glob /span span style color:#303336 . /span span style color:#303336 iglob /span span style color:#303336 ( /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 , /span span style color:#7d2727 *.c /span span style color:#303336 )) /span  span style color:#101094 for /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 dirs /span span style color:#303336 , /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#7d2727 src /span span style color:#303336 )) /span  span style color:#101094 for /span span style color:#303336 f /span span style color:#101094 in /span span style color:#303336 results /span span style color:#303336 : /span span style color:#101094 print /span span style color:#303336 ( /span span style color:#303336 f /span span style color:#303336 ) /span /code /span /span 

除了适合一行并避免在内存中不必要的列表之外 这也有很好的副作用 你可以用类似于**运算符的方式使用它 例如 你可以使用os.path.join(root, some/path/*.c )它来获取所有的.c文件具有此结构的src子目录。

分享改善这个答案

回答于2015年12月5日17:42

\"\"

FXX

806814

添加评论

1

 

Johan Dahlin的答案简化版 没有fnmatch。

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 osmatches /span span style color:#303336 /span span style color:#303336 [] /span  span style color:#101094 for /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 dirnames /span span style color:#303336 , /span span style color:#303336 filenames /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#7d2727 src /span span style color:#303336 ): /span span style color:#303336  matches /span span style color:#303336 /span span style color:#303336 [ /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 f /span span style color:#303336 ) /span span style color:#101094 for /span span style color:#303336 f /span span style color:#101094 in /span span style color:#303336 filenames /span span style color:#101094 if /span span style color:#303336 f /span span style color:#303336 [- /span span style color:#7d2727 2 /span span style color:#303336 :] /span span style color:#303336 /span span style color:#7d2727 .c /span span style color:#303336 ] /span /code /span /span 

分享改善这个答案

于2013年6月3日1:29 回答

\"\"

flowfree

13K93765

添加评论

1

 

或者列表理解

 span style color:#242729 span style color:#393318 code span style color:#303336 /span span style color:#303336 base /span span style color:#303336 /span span style color:#303336 r /span span style color:#7d2727 c:\\User\\xtofl /span  span style color:#303336 /span span style color:#303336 binfiles /span span style color:#303336 /span span style color:#303336 [ /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 base /span span style color:#303336 , /span span style color:#303336 f /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 base /span span style color:#303336 , /span span style color:#303336 _ /span span style color:#303336 , /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 f /span span style color:#101094 in /span span style color:#303336 files /span span style color:#101094 if /span span style color:#303336 f /span span style color:#303336 . /span span style color:#303336 endswith /span span style color:#303336 ( /span span style color:#7d2727 .jpg /span span style color:#303336 ) /span span style color:#303336 ] /span /code /span /span 

分享改善这个答案

于2013年6月24日10:41 回答

\"\"

xtofl

32K782161

添加评论

1

 

这是我的解决方案 使用列表解析在目录和所有子目录中递归搜索多个文件扩展名

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 os /span span style color:#303336 , /span span style color:#303336 glob /span span style color:#101094 def /span span style color:#303336 _globrec /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 , /span span style color:#303336 * /span span style color:#303336 exts /span span style color:#303336 ): /span  span style color:#7d2727 Glob recursively a directory and all subdirectories for multiple file extensions  Note: Glob is case-insensitive, i. e. for \\*.jpg you will get files ending with .jpg and .JPG Parameters ---------- path : str A directory name exts : tuple File extensions to glob for Returns ------- files : list list of files matching extensions in exts in path and subfolders /span span style color:#303336  dirs /span span style color:#303336 /span span style color:#303336 [ /span span style color:#303336 a /span span style color:#303336 [ /span span style color:#7d2727 0 /span span style color:#303336 ] /span span style color:#101094 for /span span style color:#303336 a /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#303336 path /span span style color:#303336 )] /span span style color:#303336  f_filter /span span style color:#303336 /span span style color:#303336 [ /span span style color:#303336 d /span span style color:#303336 /span span style color:#303336 e /span span style color:#101094 for /span span style color:#303336 d /span span style color:#101094 in /span span style color:#303336 dirs /span span style color:#101094 for /span span style color:#303336 e /span span style color:#101094 in /span span style color:#303336 exts /span span style color:#303336 ] /span  span style color:#101094 return /span span style color:#303336 [ /span span style color:#303336 f /span span style color:#101094 for /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 [ /span span style color:#303336 glob /span span style color:#303336 . /span span style color:#303336 iglob /span span style color:#303336 ( /span span style color:#303336 files /span span style color:#303336 ) /span span style color:#101094 for /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 f_filter /span span style color:#303336 ] /span span style color:#101094 for /span span style color:#303336 f /span span style color:#101094 in /span span style color:#303336 files /span span style color:#303336 ] /span span style color:#303336 my_pictures /span span style color:#303336 /span span style color:#303336 _globrec /span span style color:#303336 ( /span span style color:#303336 r /span span style color:#7d2727 C:\\Temp /span span style color:#303336 , /span span style color:#7d2727 \\*.jpg /span span style color:#303336 , /span span style color:#7d2727 \\*.bmp /span span style color:#303336 , /span span style color:#7d2727 \\*.png /span span style color:#303336 , /span span style color:#7d2727 \\*.gif /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 f /span span style color:#101094 in /span span style color:#303336 my_pictures /span span style color:#303336 : /span  span style color:#101094 print /span span style color:#303336 f /span /code /span /span 

分享改善这个答案

于2014年8月18日17:50 回答

\"\"

sackpower

111

添加评论

0

 

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 sys /span span style color:#303336 , /span span style color:#303336 os /span span style color:#303336 , /span span style color:#303336 globdir_list /span span style color:#303336 /span span style color:#303336 [ /span span style color:#7d2727 c:\\\\books\\\\heap /span span style color:#303336 ] /span  span style color:#101094 while /span span style color:#303336 len /span span style color:#303336 ( /span span style color:#303336 dir_list /span span style color:#303336 ) /span span style color:#303336 /span span style color:#7d2727 0 /span span style color:#303336 : /span span style color:#303336  cur_dir /span span style color:#303336 /span span style color:#303336 dir_list /span span style color:#303336 [ /span span style color:#7d2727 0 /span span style color:#303336 ] /span  span style color:#101094 del /span span style color:#303336 dir_list /span span style color:#303336 [ /span span style color:#7d2727 0 /span span style color:#303336 ] /span span style color:#303336  list_of_files /span span style color:#303336 /span span style color:#303336 glob /span span style color:#303336 . /span span style color:#303336 glob /span span style color:#303336 ( /span span style color:#303336 cur_dir /span span style color:#303336 /span span style color:#7d2727 \\\\* /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 book /span span style color:#101094 in /span span style color:#303336 list_of_files /span span style color:#303336 : /span  span style color:#101094 if /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 isfile /span span style color:#303336 ( /span span style color:#303336 book /span span style color:#303336 ): /span  span style color:#101094 print /span span style color:#303336 ( /span span style color:#303336 book /span span style color:#303336 ) /span  span style color:#101094 else /span span style color:#303336 : /span span style color:#303336  dir_list /span span style color:#303336 . /span span style color:#303336 append /span span style color:#303336 ( /span span style color:#303336 book /span span style color:#303336 ) /span /code /span /span 

分享改善这个答案

于2014年1月27日19:03 回答

\"\"

serega386

1187174

添加评论

0

 

我在这篇文章中修改了最常见的答案..最近创建了这个脚本 它将循环遍历给定目录 searchdir 中的所有文件及其下的子目录...并打印文件名 rootdir 修改/创建日期和尺寸。

希望这有助于某人...他们可以走目录并获取fileinfo。

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 time /span span style color:#101094 import /span span style color:#303336 fnmatch /span span style color:#101094 import /span span style color:#303336 os /span span style color:#101094 def /span span style color:#303336 fileinfo /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 ): /span span style color:#303336  filename /span span style color:#303336 /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 basename /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 ) /span span style color:#303336  rootdir /span span style color:#303336 /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 dirname /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 ) /span span style color:#303336  lastmod /span span style color:#303336 /span span style color:#303336 time /span span style color:#303336 . /span span style color:#303336 ctime /span span style color:#303336 ( /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 getmtime /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 )) /span span style color:#303336  creation /span span style color:#303336 /span span style color:#303336 time /span span style color:#303336 . /span span style color:#303336 ctime /span span style color:#303336 ( /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 getctime /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 )) /span span style color:#303336  filesize /span span style color:#303336 /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 getsize /span span style color:#303336 ( /span span style color:#303336 file /span span style color:#303336 ) /span  span style color:#101094 print /span span style color:#7d2727 %s**\\t%s\\t%s\\t%s\\t%s /span span style color:#303336 % /span span style color:#303336 ( /span span style color:#303336 rootdir /span span style color:#303336 , /span span style color:#303336 filename /span span style color:#303336 , /span span style color:#303336 lastmod /span span style color:#303336 , /span span style color:#303336 creation /span span style color:#303336 , /span span style color:#303336 filesize /span span style color:#303336 ) /span span style color:#303336 searchdir /span span style color:#303336 /span span style color:#303336 r /span span style color:#7d2727 D:\\Your\\Directory\\Root /span span style color:#303336 matches /span span style color:#303336 /span span style color:#303336 [] /span  span style color:#101094 for /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 dirnames /span span style color:#303336 , /span span style color:#303336 filenames /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#303336 searchdir /span span style color:#303336 ): /span  span style color:#858c93 ## for filename in fnmatch.filter(filenames, *.c ): /span  span style color:#101094 for /span span style color:#303336 filename /span span style color:#101094 in /span span style color:#303336 filenames /span span style color:#303336 : /span  span style color:#858c93 ## matches.append(os.path.join(root, filename)) /span  span style color:#858c93 ##print matches /span span style color:#303336  fileinfo /span span style color:#303336 ( /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 filename /span span style color:#303336 )) /span /code /span /span 

分享改善这个答案

于14年11月15日13:39 回答

\"\"

ihightower

114561736

添加评论

0

 

这是一个解决方案 它将模式与完整路径匹配 而不仅仅是基本文件名。

它用于fnmatch.translate将glob样式模式转换为正则表达式 然后将其与遍历目录时找到的每个文件的完整路径进行匹配。

re.IGNORECASE是可选的 但在Windows上是可取的 因为文件系统本身不区分大小写。 我没有费心编译正则表达式 因为文档表明它应该在内部缓存。

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 fnmatch /span span style color:#101094 import /span span style color:#303336 os /span span style color:#101094 import /span span style color:#303336 re /span span style color:#101094 def /span span style color:#303336 findfiles /span span style color:#303336 ( /span span style color:#303336 dir /span span style color:#303336 , /span span style color:#303336 pattern /span span style color:#303336 ): /span span style color:#303336  patternregex /span span style color:#303336 /span span style color:#303336 fnmatch /span span style color:#303336 . /span span style color:#303336 translate /span span style color:#303336 ( /span span style color:#303336 pattern /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 dirs /span span style color:#303336 , /span span style color:#303336 files /span span style color:#101094 in /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 walk /span span style color:#303336 ( /span span style color:#303336 dir /span span style color:#303336 ): /span  span style color:#101094 for /span span style color:#303336 basename /span span style color:#101094 in /span span style color:#303336 files /span span style color:#303336 : /span span style color:#303336  filename /span span style color:#303336 /span span style color:#303336 os /span span style color:#303336 . /span span style color:#303336 path /span span style color:#303336 . /span span style color:#303336 join /span span style color:#303336 ( /span span style color:#303336 root /span span style color:#303336 , /span span style color:#303336 basename /span span style color:#303336 ) /span  span style color:#101094 if /span span style color:#303336 re /span span style color:#303336 . /span span style color:#303336 search /span span style color:#303336 ( /span span style color:#303336 patternregex /span span style color:#303336 , /span span style color:#303336 filename /span span style color:#303336 , /span span style color:#303336 re /span span style color:#303336 . /span span style color:#303336 IGNORECASE /span span style color:#303336 ): /span  span style color:#101094 yield /span span style color:#303336 filename /span /code /span /span 

分享改善这个答案

于2015年6月30日15:39 回答

\"\"

悠悠球

5,38224242

添加评论

0

 

我需要一个python 2.x的解决方案 它可以在大型目录上快速运行。
我很喜欢这个

 span style color:#242729 span style color:#393318 code span style color:#101094 import /span span style color:#303336 subprocessfoundfiles /span span style color:#303336 /span span style color:#303336 subprocess /span span style color:#303336 . /span span style color:#303336 check_output /span span style color:#303336 ( /span span style color:#7d2727 ls src/*.c src/**/*.c /span span style color:#303336 , /span span style color:#303336 shell /span span style color:#303336 /span span style color:#101094 True /span span style color:#303336 ) /span  span style color:#101094 for /span span style color:#303336 foundfile /span span style color:#101094 in /span span style color:#303336 foundfiles /span span style color:#303336 . /span span style color:#303336 splitlines /span span style color:#303336 (): /span  span style color:#101094 print /span span style color:#303336 foundfile /span /code /span /span 

请注意 如果ls找不到任何匹配的文件 您可能需要一些异常处理。

分享改善这个答案

回答于17年6月23日10:20

\"\"

罗马

291211

我刚刚意识到ls src/**/*.c只有在启用了globstar选项时才有效 shopt -s globstar - 请参阅此答案以获取详细信息。 - 罗马 于17年7  月27日13:4

原文https://stackoverflow.com/questions/2186525/how-to-use-glob-to-find-files-recursively

本文链接: http://globsolution.immuno-online.com/view-730356.html

发布于 : 2021-03-25 阅读(0)
公司介绍
品牌分类
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616
官网:http://