问题:如何在python中使用当前日期和时间创建文件名?

这是一个功能代码(成功创建文件)

sys.stdout = open('filename1.xml', 'w')

现在,我正在尝试使用当前日期时间来命名文件(我不是python的专家)

filename1 = datetime.now().strftime("%Y%m%d-%H%M%S")
sys.stdout = open(filename1 + '.xml', 'w')

我想写出一个具有确切日期和时间的文件名,它是程序已经创建的xml文件,我只需要命名该文件即可。上面的代码不起作用。

[编辑] -返回错误

  File "./fix.py", line 226, in <module>
    filenames = datetime.now().strftime("%Y%m%d-%H%M%S")
AttributeError: 'module' object has no attribute 'now'

Here is a functional code (Create file with success)

sys.stdout = open('filename1.xml', 'w')

Now I’m trying to name the file with the current Date Time (I’m not an expert in python)

filename1 = datetime.now().strftime("%Y%m%d-%H%M%S")
sys.stdout = open(filename1 + '.xml', 'w')

I want to write out a file name with the exact date and time, it is a xml file, that the program has already create, I just need to name the file. The above code is not working.

[EDITED] – The error returned

  File "./fix.py", line 226, in <module>
    filenames = datetime.now().strftime("%Y%m%d-%H%M%S")
AttributeError: 'module' object has no attribute 'now'

回答 0

不使用时datetime,这可以解决您的问题(回答您的问题),该问题是使用您指定的当前时间和日期格式获取字符串:

import time
timestr = time.strftime("%Y%m%d-%H%M%S")
print timestr

Yield:

20120515-155045

因此您的文件名可以追加或使用此字符串。

While not using datetime, this solves your problem (answers your question) of getting a string with the current time and date format you specify:

import time
timestr = time.strftime("%Y%m%d-%H%M%S")
print timestr

yields:

20120515-155045

so your filename could append or use this string.


回答 1

datetime模块中类中的类方法datetime。所以你需要

datetime.datetime.now()

或者您可以使用其他导入

from datetime import datetime

通过这种方式,您可以datetime.now按照问题中的代码使用。

is a class method in the class datetime in the module datetime. So you need

datetime.datetime.now()

Or you can use a different import

from datetime import datetime

Done this way allows you to use datetime.now as per the code in the question.


回答 2

更改此行

filename1 = datetime.now().strftime("%Y%m%d-%H%M%S")

filename1 = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

注意额外的datetime。或者,将您更改 import datetimefrom datetime import datetime

Change this line

filename1 = datetime.now().strftime("%Y%m%d-%H%M%S")

To

filename1 = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")

Note the extra datetime. Alternatively, change your import datetime to from datetime import datetime


回答 3

这个更容易被人类理解。

from datetime import datetime

datetime.now().strftime("%d-%m-%Y_%I-%M-%S_%p")
'15-08-2019_11-57-48_PM'

This one is much more human readable.

from datetime import datetime

datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p")
'2020_08_12-03_29_22_AM'

回答 4

我很惊讶没有单个格式化程序返回默认的(并且安全的)“用于追加文件名”-时间的格式,我们可以简单地编写 FD.write('mybackup'+time.strftime('%(formatter here)') + 'ext'

"%x" instead of "%Y%m%d-%H%M%S"

I’m surprised there is not some single formatter that returns a default (and safe) ‘for appending in filename’ – format of the time, We could simply write FD.write('mybackup'+time.strftime('%(formatter here)') + 'ext'

"%x" instead of "%Y%m%d-%H%M%S"

回答 5

我需要在文件夹名称中包括日期时间戳,以便从Web刮板转储文件。

# import time and OS modules to use to build file folder name
import time
import os 

# Build string for directory to hold files
# Output Configuration
#   drive_letter = Output device location (hard drive) 
#   folder_name = directory (folder) to receive and store PDF files

drive_letter = r'D:\\' 
folder_name = r'downloaded-files'
folder_time = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
folder_to_save_files = drive_letter + folder_name + folder_time 

# IF no such folder exists, create one automatically
if not os.path.exists(folder_to_save_files):
    os.mkdir(folder_to_save_files)

Here’s some that I needed to include the date-time stamp in the folder name for dumping files from a web scraper.

# import time and OS modules to use to build file folder name
import time
import os 

# Build string for directory to hold files
# Output Configuration
#   drive_letter = Output device location (hard drive) 
#   folder_name = directory (folder) to receive and store PDF files

drive_letter = r'D:\\' 
folder_name = r'downloaded-files'
folder_time = datetime.now().strftime("%Y-%m-%d_%I-%M-%S_%p")
folder_to_save_files = drive_letter + folder_name + folder_time 

# IF no such folder exists, create one automatically
if not os.path.exists(folder_to_save_files):
    os.mkdir(folder_to_save_files)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。