Alex Lusby
Alex Lusby

Reputation: 1

Writing to a file [Python]

Basically I need help on how to write to a file. Then output certain parts that are in the file.

name = input("Please enter your name: ")
print ("This is level 1"
mylist=["Man utd are the best team","I am going to be a pro typer.","Coding is really fun when you can do it."]
x=random.choice (mylist)
print ("The sentence I would like you to type is: ")
print (x)

wait = input ("Please press enter to continue, The timer will start upon hitting enter!")
start = time.time()
sentence = input("Start typing: ")
end = time.time()
overall = end - start

if sentence == (x):
    print ("It took you this many seconds to complete the sentence: %s" % overall)

I want to record high scores with the name. So I want to write the time it took someone to complete the sentence to a file. Then output the best time in the file. When the program is run again the time it took is put into the file. Whichever time is best is outputted. Is this possible? Please help. I'm new to coding.

Upvotes: 0

Views: 113

Answers (3)

seanoftime
seanoftime

Reputation: 186

Since you're using python for this project, you might want to try the pickle module.

It would allow you to dump and retrieve the time as-is, without conversion to and from a string. For example:

import pickle

# Your code here

pickle.dump(overall, timefile)

Later on, when you want to retrieve the time from the file, use something like:

last_time = pickle.load(timefile)

Upvotes: 1

nonamorando
nonamorando

Reputation: 1605

Use the open() method, read(), and write() method to write to a file.

To open a file, then write to it Python has the methods:

f = open(filename, modeYouDesire)

In this case I would do this (it will handle Unix-line endings, I believe):

f = open(filename, 'rU')

Once the file is open, you will need to convert the file to a string (that is to properly write within the contents) with the .read() method:

textOfFile = f.read()

Now that it's a string use the .write() method to write a string in the file contents. textOfFile.write(whateverYouNeedToWrite)

The argument for the .write() method is a string, so be careful of what type you input into it.

Optionally you can use the .close() method to close the file at the end of the method, but it is not required.

Further docs can be found here about the file writing methods: https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

Upvotes: 1

Alex Riordan
Alex Riordan

Reputation: 114

You can open a file as:

with open(filename, mode) as alias_name:
  # lines of code...

Using the with keyword with open will always close the file on the completion of the block, so you do not need to manage the closing of files. To write to a file, you can use a mode of w. For a full list of modes see: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

with open(filename, 'w') as f:
  f.write(content_that_needs_to_be_written)

Upvotes: 5

Related Questions