在文件中苦苦挣扎

时间:2017-09-20 20:17:00

标签: python file

我正在做一个项目,我需要制作一个文本文件。但python程序没有创建一个文件,我已经搜索了许多论坛,并尝试了一切我不知道发生了什么。我在下面列出了代码和错误消息。

import time
import sys 
import os
import datetime
from pathlib import Path

cont = True
nows = time.strftime("%d/%m/%Y %H:%M")
tst = datetime.datetime.now().isoformat("-").split(".")[0].replace(":","-")
f = open("LOG %s.txt" % nows, "w+")
f.write('Start time, End time, Callsign, Report, Report sent, Serial, locator, comments')

while cont == True:
    now = datetime.datetime.now()
    callsign = input("Callsign : ")
    start = now
    print(now)
    rst_rc = input("Signal report recived: ")
    rst_st = input("Signal report semt: ")
    ser = input("Serial number: ")
    loc = input("Locator: ")
    comment = input("Comments: ")
    end = now
    print(now)
    f.write(start, end, callsign, rst_rc, rst_st, ser, loc, comment)

contin = input("Do you want another call? ")
if contin == "y":
    cont = False
    sys.exit()

以下是我得到的错误:

traceback (most recent call last):
  File "/Users/thomasscott/Desktop/Py-qso/py-qso.py", line 10, in <module>
    f = open("LOG %s.txt" % nows, "w+")
FileNotFoundError: [Errno 2] No such file or directory: 'LOG 20/09/2017 21:08.txt'

1 个答案:

答案 0 :(得分:0)

正如@juanpa指出的那样,你的日期被解释为路径,所以它试图在名为“2017 21:08.txt”的目录“LOG 20/09 /”中打开一个文件。要解决这个问题,只需在第8行的strftime调用中将“/”替换为“_”。另外,我打开模式“w +”可以创建文件。只需使用“w”。

相关问题