1

I get the error in the title of this question. How can I fix that? The posts I found on Google did it like I did but it does not work for me. Thank you for your help!

#!/usr/bin/python3

import os
import json
import datetime
import cgi
import time

def save(number_input, current_time):
    i = 0
    while os.path.exists("datei/datei{}.txt".format(i)):
        i += 1

    datei = {
        "input": number_input,
        "zeit": current_time
    }

    with open("datei/datei{}.txt".format(i), "w+") as file:
        json.dump(datei, file)

form = cgi.FieldStorage(encoding="utf-8")

number = form.getvalue("first")
time = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")

save(number, time)

print("<p>Sie haben {} in einer .txt Datei gespeichert!              </p>".format(number))                              
time.sleep(4)
print("Location: main.py")
print()

1 Answer 1

2

In this line: time = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")

You are overwritting the time variable, that contained a module and making it a string.

This is an example of working code

#!/usr/bin/python3

import os
import json
import datetime
import cgi
import time

def save(number_input, current_time):
    i = 0
    while os.path.exists("datei/datei{}.txt".format(i)):
        i += 1

    datei = {
        "input": number_input,
        "zeit": current_time
    }

    with open("datei/datei{}.txt".format(i), "w+") as file:
        json.dump(datei, file)

form = cgi.FieldStorage(encoding="utf-8")

number = form.getvalue("first")
time_str = datetime.datetime.today().strftime("%d.%m.%Y %H:%M:%S")

save(number, time)

print("<p>Sie haben {} in einer .txt Datei gespeichert!              </p>".format(number))                              
time.sleep(4)
print("Location: main.py")
print()

Notice I've changed time to time_str, this way your time variable is untouched and you can call sleep :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.