Skip to main content
EnglandComputer ScienceSyllabus dot point

How do you accept user input and read from and write to comma separated value (CSV) text files in Python?

Write programs that accept and respond appropriately to user input, and that read from and write to comma separated value text files.

A focused answer to Edexcel GCSE Computer Science 6.4.1 and 6.4.2, covering accepting and responding to user input, and reading from and writing to comma separated value (CSV) text files in Python.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Accepting and responding to user input
  3. Reading from a text file
  4. Writing to a text file
  5. Working with CSV files
  6. Try this

What this dot point is asking

Edexcel wants you to write programs that accept and respond to user input, and that read from and write to comma separated value (CSV) text files in Python, which is a core skill for the practical Paper 2.

Accepting and responding to user input

name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello " + name + ", next year you will be " + str(age + 1))

The conversion is essential: input() gives text, so age + 1 would fail or concatenate wrongly without int(). Note also that to join a number into output you convert it back with str(). Responding appropriately means doing something sensible with the input, such as checking it (validation, covered separately) or using it in a calculation.

Reading from a text file

total = 0
file = open("scores.txt", "r")
for line in file:
    total = total + int(line)
file.close()
print(total)

Reading a file is a loop over its lines. Because each line is text, numbers must be converted with int() or float() before arithmetic, just like keyboard input. Closing the file frees it for other use and ensures data is saved.

Writing to a text file

file = open("log.txt", "w")
file.write("First line\n")
file.write("Second line\n")
file.close()

The crucial point is that "w" replaces the whole file, so to keep existing content you use "a" to append. Unlike print(), write() does not add a newline, so include "\n" where you want line breaks.

Working with CSV files

file = open("members.csv", "r")
for line in file:
    fields = line.strip().split(",")
    name = fields[0]
    age = fields[1]
    print(name + " is " + age)
file.close()

The key technique is line.strip().split(","): strip() removes the trailing newline, and split(",") breaks the line into a list of fields accessed by index (fields[0], fields[1], and so on). To write a CSV line, join the fields with commas and add a newline, for example file.write(name + "," + age + "\n").

Try this

Q1. State what input() returns in Python. [1 mark]

  • Cue. A string (so it must be converted with int() or float() for numbers).

Q2. State what line.split(",") does to a CSV line. [1 mark]

  • Cue. It splits the line into a list of fields, separated at each comma.

Exam-style practice questions

Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Edexcel 20225 marksA text file called scores.txt has one whole number per line. Write a Python program that reads the file and prints the total of all the numbers.
Show worked answer →

Open the file, loop over each line, convert it to an integer and add to a total.

total = 0
file = open("scores.txt", "r")
for line in file:
    total = total + int(line)
file.close()
print(total)

Markers reward opening the file for reading, iterating over the lines, converting each line to an integer with int() (because file data is text), accumulating the total, closing the file, and printing the result. Forgetting the int() conversion is a common error.

Edexcel 20215 marksA CSV file members.csv has lines in the form name,age. Write a Python program that reads the file and prints each member's name followed by their age, for example 'Sam is 14'.
Show worked answer →

Read each line, strip the newline, split on the comma into fields, then output them.

file = open("members.csv", "r")
for line in file:
    fields = line.strip().split(",")
    name = fields[0]
    age = fields[1]
    print(name + " is " + age)
file.close()

Markers reward opening the file, splitting each line on the comma to separate name and age, accessing the two fields by index, building the output, and closing the file. Using split(",") on a CSV line is the key technique.

Related dot points

Sources & how we know this