简体   繁体   中英

Can't append a PyCharm file

I'm a newbie programmer , I'm coding this account tool for a game (Just for myself, can only run it in the console for now) To do this I needed a database but what I did was creating a Python file with my class, and another one that is interactive and it has to append some things you type to the other Python file , the one with the class, so it automatically saves on both files. At first I did this

action = input('Select an action')
if action == 'addw':
champion = input('\n'+'Select a champion \n')
f = open('/Users/tommaso/PycharmProjects/Random/Champs.py', 'a')
f.write('\n' + str(champion.title()) + r'.addw()')
print('\nA Win was added to' + ' '+ str(champion))

And it works. However, I at the end of my program (not just the code up there) it would just stop, and since I needed it to restart or at least give me the option to restart/quit, I had to use a while loop, so at first I tried if it worked with a while loop, and I did this (just a scratch):

while True:
   try:
      x = open('/Users/tommaso/PycharmProjects/Random/Champs.py', 'a')
      x.write('hello')
   except: break

And this , too, worked. So I made a big while loop with everything from my first console file, using the exact same method to append the action, but it doesn't work. Doesn't give me any error, but it doesn't append anything, here's the code:

while True:
try:
        action = input('Welcome to your own account tool.\n' + 'Select an     action:\n' + 'addw,addl\n' + 'getstats,getallstats,lanes\n\n')
        if action == 'addw':
           champion = input('\n' + 'Select a champion \n')
           f = open('/Users/Tommaso/PycharmProjects/Random/Champs.py', 'a')
           f.write('\n' + str(champion.title()) + r'.addw()')
           print('\nA Win was added to' + ' ' + str(champion))
except: Exceptaction = input('Exit / Restart')
        if Exceptaction== 'Exit':
                           break
        else:
              pass

But it doesn't work, it just doesn't append what I tell him to my file. Anyone knows why and how can I fix this? Thanks in advance!!

I would recommend writing to a text file rather than writing to the actual python file.

A python file (Python Program /Python Script) is used to carry out the tasks you desire (the actual program).

Where as a text file is commonly used to store and read the data for the python program.

Working with .txt or text files in python is really easy.

You could also use mySQL to create a Data Base to store and retrieve data but I would suggest starting with the text files for now and once you understand that method progress to mySQL, that way you will learn two common methods of storing and fetching data.

PS: Working with text files is quick and simple it will save a headache. Also look up some free PDF books via a google advanced search, you will have so much tutorials, you will not know where to start !!.

Hope this helps. Happy Coding :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM