如何获取我下载目录中特定文件的最新版本?

huangapple go评论120阅读模式
英文:

How Do i get the latest version of a specific file in my download directory?

问题

我需要从我的下载文件夹中的特定文件中提取数据。每年都会添加这个文件的新版本,但我不能通过添加文件的确切名称来提取数据,因为公司希望每次添加新文件时自动使用最新的文件,而不更改代码。以下是文件命名的示例:new_file_2020,new_file_2021,new_file_2022等。有人能确认一下我如何识别最近的文件吗?

在这项工作中很新,希望有人能将这个问题分解到最基本的层次。

英文:

I need to Pull data from a specific file from my downloads folder. There is a new version of this file added each year, however I cannot add the files exact name to pull the data through as the company wants the newest file to be used automatically each time the new file is added without changing the code. Here is an example of how the files are named; new_file_2020, new_file_2021, new_file_2022 etc. Can anyone confirm how I can identify the most recent file?

Very new to this work so hoping someone can break this down to the most basic level.

答案1

得分: 1

按照 @Mridul 的说法,你可以使用 os.listdir("path") 获取目录中的所有文件。

我制作了一个简单的代码示例,其中我获取当前工作目录的所有文件,并获取最后4位数字。如果文件中有一个20,那么它就是一个年份(20XX)。我更新一个字典,其中键是文件,值是年份。然后,我将该值与当前正在使用的文件年份进行比较。如果它更大,我返回它;如果不是,我返回当前正在使用的文件。

我的工作目录中的文件有:main.py,new_file_2020,new_file_2021和new_file_2022。返回的文件是new_file_2022。

import os
import operator

def get_last_year(files, current_file):
    dict_of_years: dict = dict()
    for file in files:
        year = file[-4:]
        if "20" in file:
            dict_of_years.update({file: year})
    last_year_file = max(dict_of_years.items(), key=operator.itemgetter(1))[0]
    if last_year_file[-4:] > current_file[-4:]:
        return last_year_file
    return current_file

if __name__ == "__main__":
    path = os.getcwd()
    files = os.listdir(path)
    current_file = "new_file_2021"
    file = get_last_year(files, current_file)
    print(file)
英文:

As @Mridul says you can use os.listdir("path") the get all the files of a directory.

I made an easy code example where i get all the files of the current working directory and i get the last 4 digits, if there's a 20 in that file, then it would be a year (20XX), i update a dictionary where the key is the file and it's value is the year then, i compare that value with the current file year which is being used. If it is bigger, i return it, if not, i return the current file that is being used.

The files that are on my working directory are: main.py, new_file_2020, new_file_2021 and new_file_2022, the file that is returned is new_file_2022

import os
import operator

def get_last_year(files, current_file):
    dict_of_years: dict = dict()
    for file in files:
        year = file[-4:]
        if "20" in file:
            dict_of_years.update({file: year})
    last_year_file = max(dict_of_years.items(), key=operator.itemgetter(1))[0]
    if last_year_file[-4:] > current_file[-4:]:
        return last_year_file
    return current_file

if __name__ == "__main__":
    path = os.getcwd()
    files = os.listdir(path)
    current_file = "new_file_2021"
    file = get_last_year(files, current_file)
    print(file)

答案2

得分: 0

你可以使用 os.listdir("<directory/path>") 查找所在文件夹中的所有文件和目录,然后可以根据需要过滤要调查的文件(基于扩展名、名称等),并使用 os.path.getctime("<file/path>") 检查每个文件的创建时间。

英文:

You can use os.listdir(&quot;&lt;directory/path&gt;&quot;) to find all files and directories in the folder you have your files in, after which you can filter out the files you want to investigate(based on extensions, names etc if needed) and use os.path.getctime(&quot;&lt;file/path&gt;&quot;) to then check the creation time for each file.

huangapple
  • 本文由 发表于 2023年3月15日 20:24:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定