0

I am currently doing a project were I need to make a text file. but the python program does not make a file I have trawled many forums and tried everything I do not know what is happening. I have included the code and error message below.

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()

Below is the error I get:

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'
Tombomb12
  • 15
  • 2
  • 2
    Your date is probably being interpreted as a directory path, i.e. "'LOG 20/09/2017 21:08.txt'` is being interpreted as `LOG /20/09` etc *as a file path*. – juanpa.arrivillaga Sep 20 '17 at 20:21
  • 2
    So, simple solution, use `nows = time.strftime("%d-%m-%Y %H:%M")`. Also, you are going to want to `.close()` your before you terminate the program, or else you might not write everything in the buffer. – juanpa.arrivillaga Sep 20 '17 at 20:29
  • Yes, I closed this as a duplicate of a shell question, but it's one with the exact same underlying problem. – Charles Duffy Sep 20 '17 at 20:41
  • Ok thank you for marking it as a duplicate. I could not find the other questions with the same issue I do apologise. – Tombomb12 Sep 20 '17 at 21:04

1 Answers1

0

As @juanpa pointed out, your date is interpretate as a path so it tries to open a file inside the directory "LOG 20/09/" with the name "2017 21:08.txt". To fix this just replace "/" with "_" inside your strftime call in line 8. Also, I dont thing the open mode "w+" can create files. Just use "w".

MegaIng
  • 7,361
  • 1
  • 22
  • 35