在文件中挣扎



我当前正在进行一个项目,我需要制作文本文件。但是,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'

正如@juanpa指出的那样,您的日期被解释为一条路径,因此它试图在目录中打开文件" log 20/09/",名称为" 2017年" 2017 21:08。TXT"。要解决此问题,只需在第8行中的strftime调用中替换"/"。只需使用" W"。

最新更新