Python-将日期戳添加到文本文件 [英] Python - Add Date Stamp To Text File

查看:129
本文介绍了Python-将日期戳添加到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python v2中,是否有一种获取日期/时间戳并将其放入创建新文本文件的方法?

In Python v2, is there a way to get a date/time stamp and put it into creating a new text file?

IE:当我要创建时一个新的文本文件并向其中写入程序的内容,它将创建一个包含时间/日期的新文本文件。

IE: When I want to create a new text file and write the contents of my program to it, it will create a new text file with the time/date in it.

感谢您的帮助。

推荐答案

import datetime

def timeStamped(fname, fmt='%Y-%m-%d-%H-%M-%S_{fname}'):
    return datetime.datetime.now().strftime(fmt).format(fname=fname)

with open(timeStamped('myfile.txt'),'w') as outf:
    outf.write('data!')

这篇关于Python-将日期戳添加到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆