-1

So when I run this code it works perfectly, but it overwrites the previous times in the stopwatch_times.txt so, I searched high and low but couldn't find out how to do it.

#!/usr/bin/python
import time

var_start = input("Press Enter To START The stopwatch")
t0 = time.time()
var_stop = input("Press Enter to STOP The stopwatch")

stopwatch_time = round(time.time() - t0,2)
stopwatch_time = str(stopwatch_time)

file_ = open("stopwatch_times.txt")
with open('stopwatch_times.txt', 'w') as file_:
    file_.write(stopwatch_time)

print ("Stopwatch stopped - Seconds Elapsed : ",round(time.time() - t0,2))
  • 2
    it overwrites since you opened the file for overwriting (`'w'`), and you're just writing the new time there. What should it do instead? – Antti Haapala -- Слава Україні Apr 11 '15 at 09:39
  • 1
    "Questions seeking debugging help ("why isn't this code working?") must include the **desired behavior**, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Antti Haapala -- Слава Україні Apr 11 '15 at 09:40
  • when i remove the ('w') it gives me an error code: Traceback (most recent call last): File "C:\Program Files (x86)\Python34\PROJECTS\Stopwatch\stopwatch.py", line 14, in file_.write(stopwatch_time) io.UnsupportedOperation: not writable – Newbieprogrammer Apr 11 '15 at 09:41
  • 2
    yes, I asked what should the program do *instead* of overwriting the file. Maybe you'd want to use `'a'` mode to append to the end of the file? – Antti Haapala -- Слава Україні Apr 11 '15 at 09:43
  • `file_.write(stopwatch_time)` is not indented properly. I guess that happened when you pasted your code here because Python would've complained about an `IndentationError: expected an indented block`. But please fix it up. – PM 2Ring Apr 11 '15 at 09:52
  • If you simply remove the `'w'` the file is opened in read mode, which is why you get the `not writable` error message. Also, as Klaus D. mentions, you should write a newline after the time string, otherwise when you append to the file all the time strings will run together on one line. Besides, some programs that read text files don't like it if the file doesn't end in a newline. – PM 2Ring Apr 11 '15 at 09:55
  • 1
    @Newbieprogrammer you still haven't amended your question to state what is the *expected* behaviour – Antti Haapala -- Слава Україні Apr 11 '15 at 10:11
  • @Newbieprogrammer: Also, you're attempting to open the file twice - is that actually in your code, or is that another typo? – PM 2Ring Apr 11 '15 at 10:11
  • @AnttiHaapala: Newbieprogrammer described the expected behaviour in a comment to Klaus D's answer. But of course that information belongs in the question itself. – PM 2Ring Apr 11 '15 at 10:15

2 Answers2

4

You have to open the file with mode 'a' to append to it:

with open('stopwatch_times.txt', 'a') as file_:
    ...  # Write to the file.

Now it will list times one after the other. If you have problem with linebreaks, make sure you add the right line break characters for your system to the line.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • I tried writing : with open('stopwatch_times.txt', 'a') as file_: and it writes like this 0.500.100.541.65 it writes the together i want to write them like this : 0.50 0.10 0.54 etc. – Newbieprogrammer Apr 11 '15 at 09:58
  • 2
    Well, formatting the output sounds like a new question. – MERose Apr 11 '15 at 10:00
  • @Newbieprogrammer: The time strings run together like that because they don't have blanks before or after them. So do `file_.write(stopwatch_time + ' ')`; or `file_.write(stopwatch_time + '\n')` to put each time string on a separate line. – PM 2Ring Apr 11 '15 at 10:04
  • Klaus, if a file is opened in text mode then `'\n'` is automatically translated into the line-ending appropriate for the host OS on writing; conversely, on reading a text file the local line-ending is translated to `'\n'`; Python inherits this behaviour from C. – PM 2Ring Apr 11 '15 at 10:43
2

Try:

open('stopwatch_times.txt', 'a')

For more information you can check out the chapter 7.2. Reading and Writing Files at https://docs.python.org/2/tutorial/inputoutput.html

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
Vlad
  • 36
  • 3
  • Beware of saying [Try: in an answer](http://meta.stackoverflow.com/q/256359/4014959). Also, [Do. Or do not. There is no try.](https://www.youtube.com/watch?v=BQ4yd2W50No) :) – PM 2Ring Apr 11 '15 at 10:47