Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一系列子文件夹中获取最后写入的文件_Python_File - Fatal编程技术网

Python 从一系列子文件夹中获取最后写入的文件

Python 从一系列子文件夹中获取最后写入的文件,python,file,Python,File,我已尝试此解决方案: 我尝试的代码是: import glob import os list_of_files = glob.glob('/path/to/folder/**/*.csv') latest_file = max(list_of_files, key=os.path.getctime) print (latest_file) 我收到了与文件的Windows时间戳日志相关的输出 但我在各自的子文件夹中维护了一个单独的日志,用于写入文件 当我打开日志时,我看到最后更新的文件不是

我已尝试此解决方案:

我尝试的代码是:

import glob
import os

list_of_files = glob.glob('/path/to/folder/**/*.csv') 
latest_file = max(list_of_files, key=os.path.getctime)
print (latest_file)
我收到了与文件的Windows时间戳日志相关的输出

但我在各自的子文件夹中维护了一个单独的日志,用于写入文件

当我打开日志时,我看到最后更新的文件不是Python代码指定的文件

我感到震惊,因为我的整个过程取决于最后一次写入的文件。
请告诉我如何通过Python获得最后更新的文件

我想读取上次更新的文件,但由于windows没有优先考虑上次修改的文件的更新,我看不到任何其他出路

是否有人有其他方法来查找它?

是文件的创建时间-似乎您想要的是文件的修改时间,因此,请尝试:

latest_file = max(list_of_files, key=os.path.getmtime)
然后看看这是否符合您的要求。

是文件的创建时间-似乎您需要的是文件的修改时间,因此,请尝试:

latest_file = max(list_of_files, key=os.path.getmtime)
在linux中,os.path.getctime()返回上次修改的时间,但在windows中返回创建时间。您需要使用os.path.getmtime在windows上获取修改后的时间

import glob
import os

list_of_files = glob.glob('/path/to/folder/**/*.csv') 
latest_file = max(list_of_files, key=os.path.getmtime)
print (latest_file)
这段代码应该适合您。

在linux中,os.path.getctime()返回上次修改的时间,但在windows中返回创建时间。您需要使用os.path.getmtime在windows上获取修改后的时间

import glob
import os

list_of_files = glob.glob('/path/to/folder/**/*.csv') 
latest_file = max(list_of_files, key=os.path.getmtime)
print (latest_file)

这段代码应该适合您。

您是在使用
os.path.getmtime
(修改时间)而不是
getctime
(创建时间)吗?哦。。。我想这就是问题的解决办法……:):在
os.path.getmtime
(修改时间)而不是
getctime
(创建时间)之后对您进行排序?哦。。。我想这就是问题的解决办法……:):谢谢你的回答。我比你高。但请原谅我,因为这次我想给新撰稿人一个机会。我想接受汤姆的回答作为鼓励。谢谢你的回答。我比你高。但请原谅我,因为这次我想给新撰稿人一个机会。我想接受汤姆的回答作为鼓励。