如何使用python获取最新创建的文件名而不是文件夹名?

如何使用python获取最新创建的文件名而不是文件夹名?,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,下面的代码运行良好,并提供了文件路径所需的输出 import glob import os list_of_files = glob.glob('/path/to/folder/*') latest_file = max(list_of_files, key=os.path.getctime) print latest_file 但是,如果创建了文件,则会给出文件路径;如果创建了文件夹,则会给出文件夹路径。而我期望的只是文件,而不是在特定文件夹中创建的文件夹 请建议我应该做什么,只获取最新创

下面的代码运行良好,并提供了文件路径所需的输出

import glob
import os

list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
但是,如果创建了文件,则会给出文件路径;如果创建了文件夹,则会给出文件夹路径。而我期望的只是文件,而不是在特定文件夹中创建的文件夹


请建议我应该做什么,只获取最新创建的文件路径,而不是最新创建的文件夹路径

如果您已经导入操作系统,则不需要任何其他模块来实现这一点。操作系统有一个名为
path
的模块,用于处理与路径相关的函数


要检查路径是目录还是文件,可以检查
os.path.isfile('/path/here')
,它将返回布尔值
true
false
,具体取决于传递的参数是否为文件,如果已经导入操作系统,则不需要任何其他模块来实现这一点。操作系统有一个名为
path
的模块,用于处理与路径相关的函数


要检查路径是目录还是文件,可以检查
os.path.isfile('/path/here')
,它将返回布尔值
true
false
,具体取决于传递的参数是文件还是非文件

,您可以使用lambdas使用类似的内容

filelist = os.listdir(os.getcwd())
filelist = filter(lambda x: not os.path.isdir(x), filelist)
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
完整的答案贴在这里

您可以使用lambdas使用类似的内容

filelist = os.listdir(os.getcwd())
filelist = filter(lambda x: not os.path.isdir(x), filelist)
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
完整的答案贴在这里

试试这个:

import glob
import os

def check_file_status(path,direc=None):
    if direc==None:
        list_of_files = glob.glob(path)
        latest_file = max(list_of_files, key=os.path.getctime)
        return latest_file
    else:
        new_path = direc+'/*'
        list_of_files = glob.glob(new_path)
        latest_file = max(list_of_files, key=os.path.getctime)
        return latest_file

if __name__ =="__main__":

    path = '/your path to file/*'
    if os.path.isfile(check_file_status(path)):
            print(latest_file)

    elif os.path.isdir(check_file_status(path)):
            add_into_path = check_file_status(path)
            print(check_file_status(path,add_into_path))

试试这个:

import glob
import os

def check_file_status(path,direc=None):
    if direc==None:
        list_of_files = glob.glob(path)
        latest_file = max(list_of_files, key=os.path.getctime)
        return latest_file
    else:
        new_path = direc+'/*'
        list_of_files = glob.glob(new_path)
        latest_file = max(list_of_files, key=os.path.getctime)
        return latest_file

if __name__ =="__main__":

    path = '/your path to file/*'
    if os.path.isfile(check_file_status(path)):
            print(latest_file)

    elif os.path.isdir(check_file_status(path)):
            add_into_path = check_file_status(path)
            print(check_file_status(path,add_into_path))


您可以使用os.path.isfile(“文件名”)或os.path.isdir(“directname”)对任何一个文件进行筛选that@Narendra我需要应用check命令的地方。我不知道我可以把它放在我的代码里的什么地方。请解释一下。请查看我的答案,如果有更新,可能会对您有所帮助。您可以使用os.path.isfile(“文件名”)或os.path.isdir(“directname”)对任何一个进行筛选that@Narendra我需要应用check命令的地方。我不知道我可以把它放在我的代码里的什么地方。请解释一下。请查看我的答案,如果有更新,可能会帮助您指定我需要搜索的文件夹。。请给我举个例子好吗?将“os.getcwd()”更改为当前/必需的目录
WindowsError:[错误2]系统找不到指定的文件:'.gitignore'
这就是我得到的错误。。如果我在我的文件夹中有这个文件。添加另一个过滤器,不要考虑隐藏文件和过滤器开始与“。”后第二行应该在同一行下面的代码>文件名=过滤器(lambda x:不是x.StastSuff'('.'),文件名)< /代码>请检查语法。一旦目录被过滤,您可以添加为第三行。在这里,我指定了需要搜索的文件夹。。请给我举个例子好吗?将“os.getcwd()”更改为当前/必需的目录
WindowsError:[错误2]系统找不到指定的文件:'.gitignore'
这就是我得到的错误。。如果我在我的文件夹中有这个文件。添加另一个过滤器,不要考虑隐藏文件和过滤器开始与“。”后第二行应该在同一行下面的代码>文件名=过滤器(lambda x:不是x.StastSuff'('.'),文件名)< /代码>请检查语法。当目录被过滤后,您可以添加为第三行。如果它不是文件,那么它不会给出最新的文件名。。。我想有最新创建的文件名任何方式。。。请你检查一下。它应该是文件或目录的权利,然后,而不是“isfile”你可以使用“isdir”没有。。。只有文件没有目录。。。我只想要最新创建的文件。。即使创建了目录,我也应该得到文件而不是目录。如果它不是文件,那么它就不会给出最新的文件名。。。我想有最新创建的文件名任何方式。。。请你检查一下。它应该是文件或目录的权利,然后,而不是“isfile”你可以使用“isdir”没有。。。只有文件没有目录。。。我只想要最新创建的文件。。即使创建了dir,我也应该得到文件而不是目录。