0

I am trying to print some of the results of my algorithm (score) to a .txt file to have that data for further analysis. Here, the algorithm shall create the file and then open it to write the number down. Then I thought about closing it again. My problem here is, that I don´t even find the file. If I create one by my own, and only try to write the number, that doesn´t work as well.

This is for the analysis of Reinforcement Learning for a robot. The scores are symbolizing Q-values and are important for further analysis. Score is here a random number.

if __name__ == '__main__':
    open('try.txt', 'w+').close()

    for e in range(agent.load_episode + 1, EPISODES):
        ...
        for t in range(agent.episode_step):
           ...
            if done:
               ...
                saveFile = open('try.txt','w')
                saveFile.write(str(score))
                saveFile.close()


From the first part I try to create a new file called try.txt (I only create the file once). Them after, I open the file, write something and close it again. When the next Q-value is calculated, the file is opened again.

clubby789
  • 2,543
  • 4
  • 16
  • 32
trello123
  • 15
  • 4
  • Opening a file in write mode will completely clear its contents. By opening and closing it for each item, you'll only write the last item. Additionally, use ```w+``` to create the file if it doesn't exist. – Michael Bianconi Aug 08 '19 at 15:27
  • 2
    see [here](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python) on how to append to files. `open('try.txt', 'w+').close()` is unnecessary. – FObersteiner Aug 08 '19 at 15:28

2 Answers2

0

Try changing saveFile = open('try.txt', 'w') to with open('try.txt', 'a+') as saveFile:

oroy
  • 82
  • 16
  • 1
    imho, the bettter syntax to use is `with open('try.txt', 'a+') as saveFile:` - that avoids getting errors if you forget to call `saveFile.close()`. – FObersteiner Aug 08 '19 at 15:29
  • @MrFuppes Changed – oroy Aug 08 '19 at 15:34
  • ok. Also note that you do not have to create the file in a separate step. `open()` takes care of that, see [here](https://docs.python.org/3/library/functions.html#open). I know, Python can be confusing if you come from something like `C` ;-) – FObersteiner Aug 08 '19 at 15:49
  • Thank you @MrFuppes – oroy Aug 08 '19 at 15:54
0

Should the file contain only the last calculated value, all the values (possibly each in new line) from single run, or even values through separate runs? Nevertheless, this, a bit modified, snipped might be what you are looking for:

if __name__ == '__main__':
    with open('try.txt', 'w') as saveFile:  # change to 'a' if you want the results to be stored between runs
        for e in range(agent.load_episode + 1, EPISODES):
            ...
            for t in range(agent.episode_step):
                ...
                if done:
                    ...
                    # saveFile.truncate()  uncommenting this means that the file only stores the latest value
                    saveFile.write(str(score) + '\n')  # write each result to new line
                    saveFile.flush()  # this line makes the results accessible from file as soon as they are calculated

In python with is the preferred method of opening files, as it takes care of closing it at the right moment. When opening file in 'w' mode the caret inside the file is placed at the beginning of file and if a file had any data in it, it gets erased.

The 'a' mode appends to file. You may want to take a look at this.

Now I believe that you wanted to open and close the file on and on, as to have the data accessible as soon as the iteration is finished. That is what saveFile.flush() is for. Please let me know if this helps you!


To better control where the file gets created take use of os module:

import os
directory = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(directory, 'try.txt')
# print(file_path)
with open(file_path, 'w') as saveFile:
TheDecks
  • 86
  • 3
  • Thanks for the answer! Okay, I got that with the different possibilities with a, w etc.. The code shall save the scores everytime when a new score is calculated (if done). That´s why I changed it to a now. Is there also an opportunity to create the file in the code, so that I only have to name it in the code and then it is created? Thanks for your help! – trello123 Aug 09 '19 at 07:37
  • And one other question is now, can I open the .txt file while the code is running to get access to the storad data?Thanks for your help again! – trello123 Aug 09 '19 at 07:46
  • As for the first question, python handles it by himself. When you invoke open('try.txt', 'w') the file gets created if it is not found, so you don't have to worry about it. And yes, you can open the file, copy from it etc. I believe that you shouldn't modify it though. Please also note, that it probably won't update itself *live*. So if the calculation is done and you have the file opened in your chosen text editor, you might want to reopen it. Alternatively you may use a real time viewer like mTail. **EDIT:** If you open the file in Notepad++ it will notify you that it has been modifed. – TheDecks Aug 09 '19 at 08:18
  • Thanks for the fast answer. Somehow it doesn´t work for me. I now copy pasted your code from above, with the 'w', but I can not find the try.txt file in my folder. Is it necessary to also provide the path to the directory? I thought it is saved in the directory where my script is saved as well. – trello123 Aug 09 '19 at 09:01
  • Check edited answer to see how to make sure the file is created in right directory. Uncomment the print statement to print the information to console. – TheDecks Aug 09 '19 at 09:26
  • That´s strange. It just won´t create the file. I can´t find it in the directory folder nor somewhere else. Kinda strage that whole thing. I am using ubuntu 16.06, but that shouldn´t affect anything :/ – trello123 Aug 09 '19 at 09:40
  • Possibly changing 'w' to 'w+' could do the trick. Please see: https://stackoverflow.com/questions/2967194/open-in-python-does-not-create-a-file-if-it-doesnt-exist for more info and possibly more solutions. – TheDecks Aug 09 '19 at 09:49
  • 1
    Okay it just started working. My problem was within the directory path. I launch the whole script with a .launch file. That might have interrupted something. With with open('/home/ifl/catkin_ws/src/turtlebot3_machine_learning/turtlebot3_dqn/nodes/doubledqn_stage_1_1.txt', 'w+') as saveFile: it worked fine :) Thanks a lot again! – trello123 Aug 09 '19 at 13:48