我正在尝试让python在完成其设计目的的报告后自动将当前时间和日期添加为文件名。
答案 0 :(得分:1)
使用datetime.date.today()
从datetime模块获取当前日期,使用strftime()
将其解析为所需格式,然后将其添加到文件名字符串中。
import datetime
filename = 'XYZ Report {0}.txt'
current_date = datetime.date.today().strftime('%d %b %Y')
filename = filename.format(current_date)
# filename = XYZ Report 19 Sep 2018.txt
with open(filename) as file_obj:
# File writing logic
答案 1 :(得分:0)
import time
import xlwt
import csv
date_string = time.strftime("%Y%m%d %H.%M")
data_output = []
with open('C:\\Users\\Desktop\\File Name' + date_string + '.csv', 'a+') as f:
w = csv.writer(f, delimiter=',')
for data in data_output:
w.writerow(data)
f.close()
df.to_excel('C:\\Users\\Desktop\\File Name' + date_string + '.xlsx')
df.to_csv('C:\\Users\\Desktop\\File Name' + date_string + '.txt')