How can I write to the file and then read it?

Suppose there is a line 1 in the file "Password". When you start the program asks for a password and when you enter what is written in the settings file, the program performs the actions further. (I have already done this), but I still need to ask the user what OS he is after entering (naturally correct). And after the answer, Suppose 1. Win XP 2. Win 7, etc. The second line was written to the file, for example, User OS = Win XP, Win 7, etc. And after the answer, certain actions related to the OS were performed. Suppose if the value User OS = Win XP then the win_xp_init () function is executed. Which displays the message Hello user on the Windows xp. Sorry forgot forgot to add to Python 2.7

  • Once again - psychics on vacation! - Artem
  • What to write then? On the monitor? - den94
  • aha, drive a finger! :) on the touchscreen. - Artem

2 answers 2

Let's try the telepathy miracle ...

Probably in your case (modification of the configuration file) it would be most appropriate to open the file-2 record (it is better to generate a unique name), copy the necessary data into it from the original file.conf , and then add new ones.

After closing the files, rename them: file.conf to file.conf.bak, and file-2 to file.conf

The specific characters (typed in the editor) depend on the programming language and OS you are using.

    Here is an option in the forehead using the configuration file:

    # -*- coding: utf-8 -*- from ConfigParser import SafeConfigParser def windows(): print "User has Windows" def linux(): print "User has Linux" if __name__ == '__main__': parser = SafeConfigParser() parser.read("test.conf") password_answer = raw_input("Enter password: ") if not password_answer == parser.get("test", "password"): print "Wrong password!" exit() os_answer = raw_input("Enter your OS: ").lower() if os_answer == "windows": windows() elif os_answer == "linux": linux() else: print "Your OS ain't supported" parser.set("test", "user_os", os_answer) parser.write(open("test.conf", "w")) 

    This is the test.conf file:

     [test] password = test