0

如果超过 6 天,我想删除并替换同名的 Excel 文件。os.path.getctime(file_path)但是,当我根据 [ ][1]评估文件的年龄时,getctime删除并替换它后该值不会改变。

我不确定是否使用getmtimegetatime会起作用,因为我将在此期间修改文件。

    import os #check existing excel file stats
    import time #find creation date of file
    import openpyxl #excel module
    import sys #to collect sys args from command line

    def establish_wb():
        file_path = r'C:\Users\cj9250\Documents\this_weeks_blue_stakes.xlsx'

        if os.path.exists(file_path) == False: #if excel file does not exist
            wb = openpyxl.Workbook()
            wb.save(file_path)#create it

        current_time = time.time()
        creation_time = os.path.getctime(file_path) #when file was created
        file_age = (current_time - creation_time)/86400 #number of days since
        print(file_age)
        if file_age > 6: #if older than 6 days, replace the file
            os.remove(file_path)
            wb = openpyxl.Workbook()
            wb.save(file_path)#create file
            print('new one')
        else:#if younger than 6 days use existing one
            wb = openpyxl.load_workbook(file_path)
            print('old one')

        return wb



  [1]: https://docs.python.org/2/library/os.path.html

我最终通过将文档创建时间存储在随机单元格中来解决这个问题。

import os #check existing excel file stats
import time #find creation date of file
import openpyxl #excel module
import sys #to collect sys args from command line

def establish_wb():
    file_name = 'this_weeks_blue_stakes.xlsx'
    file_path = 'C:\\Users\\cj9250\\Documents\\' + file_name

    if os.path.exists(file_path) == False: #if excel file does not exist
        wb = openpyxl.Workbook()
        wb.active['a49'] = time.time() #record creation time
        wb.save(file_path)#create it
        return wb #end function?
    else:#if it already exists, give wb a value for upcoming var assignments
        wb = openpyxl.load_workbook(file_path)
        print('a file existed')

    current_time = time.time()
    creation_time = wb.active['a49'].value #when file was created
    file_age = (current_time - creation_time)/86400 #number of days since creation
    print('this is the age ' + str(file_age))

    if file_age > .0005: #if older than 6 days, replace the file
        os.remove(file_path) #delete the file
        wb = openpyxl.Workbook()
        wb.active['a49'] = time.time()
        wb.save(file_path)#create file
        print('made a new new one')
    else:#if younger than 6 days use existing one
        wb = openpyxl.load_workbook(file_path)
        print('used the old one')

    return wb
4

1 回答 1

0

由于 os.path.getctime 处理对 inode 的更改,这可能是由于分配 inode 的方式。

在大多数文件系统上,如果您删除一个文件然后创建另一个文件,则通过删除前一个文件释放的 inode 将分配给新文件(假设在删除和新文件创建操作之间没有创建新文件)。

touch temp.txt
stat -c%I temp.txt
970724

rm temp.txt

touch temp.txt
stat -c%I temp.txt
970724

rm temp.txt

touch temp1.txt
stat -c%I temp1.txt
970724
touch temp.txt
stat -c%I temp.txt
970727

当您执行 os.path.getctime 时,它​​会显示上次更改文件元数据的时间。os.path.getmtime 提供有关文件内容更改信息的信息,例如读取和写入。

注意:并不总是可以获得文件创建的确切日期和时间,因为文件权限的更改可能会更改元信息。

于 2018-07-20T19:31:36.763 回答