D
D
dmitrylogvinov2017-05-07 15:07:34
Python
dmitrylogvinov, 2017-05-07 15:07:34

How to save information about entered messages in different files?

I'm writing a bot to find timetables. To send a request, you need the names of cities. I save them in separate files, however, the last city is written to both files. In words, I understand well why this happens, but I don’t know how to fix everything and write each city to a separate file, due to my almost zero knowledge of python

@bot.message_handler(content_types=['text'])
def handle_text(message):
    potp = message.text
    fmas = open('otprav.txt', 'w')
    fmas.write(potp)
    from constants import libArray
    fmas = open('otprav.txt', 'r')
    findIt = fmas.read()  # Значение, которое ищется
    if findIt == 'Создать расписание':
        bot.send_message(message.from_user.id, 'Выберите город отправления')
    if findIt in libArray:
        fromcity = libArray[findIt]
        print(fromcity)
        if bot.send_message(message.from_user.id, 'Отлично! Теперь выберите город прибытия.'):
            ppr = message.text
            tmas = open('prib.txt', 'w')
            tmas.write(ppr)
            from constants import libArray
            fmas = open('prib.txt', 'r')
            findt = fmas.read()  # Значение, которое ищется
            if findt in libArray:
                tocity = libArray[findIt]
                print(tocity)
    if potp not in libArray and not potp == 'Создать расписание':
        bot.send_message(message.from_user.id, 'К сожалению, этот город еще не обслуживается')

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimonchik, 2017-05-07
@dimonchik2013

master the design a lot of things will be decided

X
xdgadd, 2017-05-07
@xdgadd

Better use a database (eg sqlite3), it's much more convenient (and faster) than parsing text files.

N
NaName, 2017-05-07
@NaName

No fmas.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question