3_2_7 Input/output and file handling

You should be able to:

  • Be able to obtain user input from the keyboard.
  • Be able to output data and information from a program to the computer display.
  • Be able to read/write from/to a text file.

REVISE:

Input and Output

Revisit:

Read / Write from / to a text file

NOTE: You must have the text file and the python file saved in the same location if you want them to work together!

WRITING TO A NEW FILE

This line of code simply creates a new text file called newfile.txt and opens it in WRITE format. If you run this code it will create a new text file in the same location that you have saved your Python file.

newFile = open("newfile.txt","w")

Opening a file in WRITE format means that it will completely wipe anything that is already saved in there.

Now try this:

newFile = open("newfile.txt","w")
newFile.write("Hello, world!")
newFile.close() # you must close the file once you have finished with it!

Find the file that has been created in your folder and see what it says inside!

READING A FILE

If you add these two lines of code then you will see that it will read the contents of the text file and display it on the screen.

newFile = open("newfile.txt","r")
print (newFile.read())

READING A PRE-MADE TEXT FILE

Download the text file below or create your own simple text file in notepad.

Make sure that the file is saved with .txt at the end of it.

Make sure the file is saved wherever your Python file is saved.

https://drive.google.com/open?id=0B5fLtQ0Xgr2PeFdhVkRCaG9rSlU

Now run this code:

someNumbers = open("numbers.txt","r")
print (someNumbers.read())

READING LINES FROM A FILE

Download the text file below or create your own simple text file in notepad. It must have several lines.

Make sure that the file is saved with .txt at the end of it.

Make sure the file is saved wherever your Python file is saved.

https://drive.google.com/open?id=0B5fLtQ0Xgr2PQnl2Y2o4Nlo3dU0

Now run this code:

f = open('validyear.txt', 'r')
for line in f:
    print (line)

It will read each line and add "\n" to the end of the line which is a line space. This is why it appears on screen with a line space. You can remove this by doing:

f = open('validyear.txt', 'r')
for line in f:
    print (line.strip("\n"))

AN EXTRA ONE FOR A BIT OF FUN

You can export data to a .csv file which means that it can then be edited using a spreadsheet.

Think back to the last outcome and the records that you made to store the scores.

scores = [
        ["PLAYER", "SCORE"],  
        ["DinoFish", "23"],
        ["JungleQ", "94"],
      ]
scoresheet = ""

for x in range (0,len(scores)):
    scoresheet+=(",".join(scores[(x)])+"\n")

scoreFile = open("SCORES.csv","w")
scoreFile.write(scoresheet)
scoreFile.close()

This code takes the records and turns them into one long piece of string. It then writes that string to a .csv file. If you run this code and check your work folder then you will see a new file called SCORES with the scores displayed as a table.

PROGRAMMING CHALLENGE:

Username and Password Storage Program:

You will be creating an application that stores all of the usernames and passwords that a person uses for their online accounts.

The application will ask the user for:

  • A username
  • A password

The application should continue to ask for usernames and passwords until the user has finished entering all of their usernames and passwords.

The usernames and passwords should be stored in an appropriate data structure.

The application must:

  • Export the usernames and passwords to a .csv file

Extending the application - The Extra Mile

  • Before exporting the usernames and passwords, the text should be encrypted!

TEST:

  1. Download and print the test paper here: https://drive.google.com/open?id=0B5fLtQ0Xgr2PeGJ1UkxtcldfYnM
  2. Try the mock test yourself.
  3. Use the 3.2.7 Walking Talking Mock below to guide you through answering the questions.

SOURCE RECOGNITION - PLEASE NOTE: The examination examples used in these walking talking mocks are samples from AQA from their non-confidential section of the public site. They also contain questions designed by TeachIT for AQA as part of the publicly available lesson materials.