[英]Python - How to specify an order in fnmatch()?
I am trying to create for
loop in Python, using fnmatch
to catch all desired files in directory.我正在尝试在 Python 中创建
for
循环,使用fnmatch
来捕获目录中的所有所需文件。 Due to further merging of files, I would require to run this loop based on file names (eg run file 20180301 through code before 20180302 etc.).由于文件的进一步合并,我需要根据文件名运行此循环(例如,通过 20180302 之前的代码运行文件 20180301 等)。
Currently I simply use the following line to get files:目前我只是使用以下行来获取文件:
for file_name in fnmatch.filter(os.listdir(source_dir), "Test_" + instance + "_*.csv"):
Is it possible to create some sort of sorting within fnmatch
?是否可以在
fnmatch
创建某种排序? I found some options to do that with glob
, but would prefer to stick with fnmatch
if possible (as otherwise there will be quite some changes required within code).我找到了一些使用
glob
来做到这一点的选项,但如果可能的话,我更愿意坚持使用fnmatch
(否则代码中将需要进行相当多的更改)。
为什么不是这个:
for file_name in sorted(fnmatch.filter(os.listdir(source_dir), "Test_" + instance + "_*.csv")):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.