top of page

Python 10c - REMOVE & Edit LINES

noun-documents-6053340-FFFFFF.png

Splitting a File

The split command is used to split up a line of a file into different parts.

​

The character or string in brackets after the split command is the value that will denote each split.

​

In the example below I have split the file at each comma. Remember that Python numbering starts at 0 so the first section is 0, not 1.

py100.png
py99.png
py101.png

0

1

2

3

The program below splits each line of the file at each forward-slash ( ).

​

The printed statement is the employee's first name, surname and job position.

py95.png

0

1

2

3

4

py97.png
py96.png

Practice Task 1

Create a file (new txt document in Notepad) called movies.

​

Type in the movie name, main actor, genre (e.g. horror), year it was released and your rating out of 10.

​

Print just the movie name and year it released.

Example solution:

py102.png
py108.png

Deleting Lines in a File

Exact Line Name

The code below shows how to remove a line from a file using the exact name of the line, which will only work for short or simple files.

​

First open the file in read move to save each line in a variable I've named lines. Then ask the user to input the exact line they want to remove (e.g. 'plum' in my example).

​

Then open the file in write mode and use a for loop to read each line and only write it back into the file if it isn't equal to the line the user entered - such as 'plum'.

​

The line.rstrip() command is important as it removes any spaces or empty lines that may interfere with matching the line to the input.

file1.png
file2.png
file4.png
file3.png

Deleting Lines in a File

Word in the Line

The code below shows how to remove a line from a file if a certain word appears in that line, although this could be dangerous with large files. In my example I have written apple which has also removed pineapple!

​

The difference from the previous program is to change the for loop so that it checks if the inputted word appears in the line.

 

  • If it does appear then nothing happens (except a print statement to acknowledge it's been found).

  • If the word doesn't appear then that line can be safely rewritten to the file.

file12.png
file5.png
file11.png
file8.png

Practice Task 2

Download the trees text file.

​

Give the user a choice of removing a specific tree or a type of tree.

​

If they choose a specific tree then remove the line if it is an exact match (e.g. Field Maple).

​

If they choose to remove a type of tree remove all lines that contain the name of that tree (e.g. willow)

​

Make sure you actually check the file to see if the lines have been removed correctly!

Example solution:

file9.png
file10.png

Download the trees file:

Sorting a File

Sorting a file into alphabetical (or numerical) order is a simple process.

​

Open the file in read mode and save the lines into a list.

​

The sort command will automatically order the list of lines. If necessary, in the brackets type reverse = True to sort the list in reverse.

file14.png
file13.png
file8.png
file19.png

Practice Task 3

Expand on your tree program from the previous practice task.

​

As well as SPECIFIC or TYPE, allow the user to enter SORT to sort the tree file either in alphabetical order or reverse alphabetical order.

​

Check the text file to see if it has been sorted correctly.

​

You may make this a separate program from task 2 if you wish.

Example solution:

file17.png
file18.png
file16.png
file15.png

Editing Lines in a File

Overwriting data in a file is a tricky process.

​

The program below uses the same Employees.txt file as above but allows the user to change the address of an employee.

​

A temporary file is created to store the lines of the employee file, but the line with the changes is replaced specifically with the new address.

 

I have explained each line of the program to the right: 

py103.png

When I executed the program below I entered Thomas Wynne's details and changed his address.

​

When I opened the employees file the address had been updated:

py105.png

1: Importing os allows me to rename and remove files later in the program.

​

3: Opens the employee file in read mode.

​

5 - 8: Input lines allow the user to enter the first name, surname and the person's new address.

​

10: A found flag is set up and set to False.

​

12: The for loop cycles through each line in the file.

​

13: Each line is split into separate parts from each /.

​

15: An if statement checks if the first name and surname match an employee in the file.

​

16: A replacement line is created by putting the original line together but with the new address.

​

18: The found flag is changed to True because the employee first name and surname matched.

​

19: A temporary file is created and opened in write mode.

​

20: The seek command restarts the file at line 0.

​

22: The for loop cycles through each line of the employee file from the beginning. If the first name and surname match it will write the new line to the file, otherwise it will rewrite the original line.

​

28 & 29: Both files are closed.

​

31 & 32: If the names didn't match, an appropriate message is printed.

​

34  - 37: If the address was changed, the original file is renamed and deleted and the temp file is renamed as the original file.

py104.png
py106.png

Practice Task 4

Use the movie file you created for practice task 1.

​

Ask the user to enter the name of a movie. Ask them to enter an updated rating out of 10.

​

Update the file to change the rating to the new value.

Example solution:

py107.png
py108.png
py109.png
bottom of page