如何打印以python开头的最新文件?
How to print latest file that starts with using python?
是否可以使用startswith
打印最新的文件?示例:以 "DOG"
开头
import subprocess
import os
import glob
list_of_files = glob.iglob("C:\Users\Guest\Desktop\OJT\scanner\*")
latest_file =
print latest_file
我不知道你所说的 startswith
是什么意思,但是试试这个:
files = glob.iglob(r"C:\Users\Guest\Desktop\OJT\scanner\*")
latest_file = max(files, key=os.path.getctime)
对目录中的每个文件进行 os.path.getctime
之类的系统调用可能既缓慢又昂贵。使用 os.scandir
在一次调用中获取目录中所有文件的信息效率要高很多倍,因为它在获取目录列表的调用过程中随时可用。
import os
directory = r"C:\Users\Guest\Desktop\OJT\scanner"
latest_file = max(os.scandir(directory), key=lambda f: f.stat().ST_MTIME).name
详情请阅读PEP-471。
是否可以使用startswith
打印最新的文件?示例:以 "DOG"
import subprocess
import os
import glob
list_of_files = glob.iglob("C:\Users\Guest\Desktop\OJT\scanner\*")
latest_file =
print latest_file
我不知道你所说的 startswith
是什么意思,但是试试这个:
files = glob.iglob(r"C:\Users\Guest\Desktop\OJT\scanner\*")
latest_file = max(files, key=os.path.getctime)
对目录中的每个文件进行 os.path.getctime
之类的系统调用可能既缓慢又昂贵。使用 os.scandir
在一次调用中获取目录中所有文件的信息效率要高很多倍,因为它在获取目录列表的调用过程中随时可用。
import os
directory = r"C:\Users\Guest\Desktop\OJT\scanner"
latest_file = max(os.scandir(directory), key=lambda f: f.stat().ST_MTIME).name
详情请阅读PEP-471。