我在Windows 8 bit-64上使用Python v2.x.
问题是,我无法生成一个txt名为实时的文件.
请看我现在的代码:
import sys
import datetime
def write():
# try:
currentTime = str(datetime.datetime.now())
print currentTime #output: 2016-02-16 16:25:02.992000
file = open(("c:\\", currentTime, ".txt"),'a') # Problem happens here
print >>file, "test"
file.close()
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法来修改行文件= open(("c:\ ....))但无法创建文本文件,如2016-02-16 16:25:02.992000.txt
有什么建议吗?
在Windows中,:文件名中是非法字符.您永远不能创建具有类似名称的文件16:25:02.
此外,您传递的是元组而不是字符串open.
试试这个:
currentTime = currentTime.replace(':', '_')
file = open("c:\\" + currentTime + ".txt",'a')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
777 次 |
| 最近记录: |