Skip to content

Python Course – 8. Files

Learn how to work with files and directories in Python: paths, reading and writing files, plus practical exercises with video lessons and downloadable theory.

We program so that a machine manages data for us, and the way we want it to. Files let us store as much data as we want: some files with information before it is processed and others with the information after it has been processed; that is why they will be one of the main inputs and outputs of our programs.

Theory

Skip to PDF content

8.1- Files and Directories

An operating system (Windows, Linux, Mac, Android, etc.) manages data with files, and these are organized in directory structures. Knowing how to manage files and directories is a crucial point when working with them.

Note

To learn more, see the following posts:

8.2- Paths

A path is the route to follow through the directories to reach one or more files. With the Path method, it will be very easy and practical to work with as many files as we want.

Exercises

Exercise 8.1-crear_fichero_y_leer_fichero.py

Console capture showing the file contents in two parts: the numbers 4, 7, 4, 2, 5 and then two appended lines of text

Using Python code, create a directory in a location on your computer and a file inside it.

Inside the file, write five lines of random numbers from 1 to 10 (Part 1).

Then read that file and print each line to the screen.

Next, add a couple of lines of text at the end of the file (Part 2).

And read the file again.

Exercise 8.2-futbolistas.py

Given the file “futbolistas.txt”, in which each line contains the data of a soccer player, you are asked to:

  1. Implement a function called “file_to_list(file_dir)” that, given the path of the file futbolista.txt, returns a
    list in which each element corresponds to a line of the file; that is, each element represents the data of one player.
  2. Implement a function called “get_top_goals_players(list_players, top_n)” that, given the list of players,
    returns in another list the “top_n” top-scoring players.
  3. Implement a function called “get_players_by_team(list_players, team)” that, given the list of players, returns
    in another list all the players who have played for the team passed as a parameter (team), sorted in descending order by the number of minutes they have played.
  4. Implement a function called “write_list_to_file(list_to_write, file_dir)” that, given the list passed as a
    parameter (list_to_write), writes each element of the list on its own line in the file passed as the parameter “file_dir”.
  5. Write the lists created in points b and c to a file, using the function implemented in point d.

In the futbolistas.txt file, each line contains the data of a soccer player, and the data are separated by the separator “::”. The data at each position are the following:

1id14PartidosCompletos27Numligas
2Apodo15PartidosSuplente28NumCopasDelRey
3Nombre16PartidosSustituidos29NumSuperCopas
4FechaNacimiento17partidosPorTemporadas30NumChampions
5LugarNacimiento18PorcentajePartidos31NumUEFAs
6Pais19MinutosJugados32NumEuropaLeagues
7Altura20MinutoPorPartido33NumSuperCopasEuropa
8Peso21Goles34NumMundialesClubes
9Demarcacion22TarjetasAmarillas35NumIntercontinentales
10NumTemporadas23TarjetasRojas36NumMundiales
11NumEquipos24PartidosSeleccionEspaniola37Numeurocopas
12PartidosJugados25GolesSeleccionEspaniola38ValorMercado
13PartidosTitular26Equipos  

“Python Course – 8. Files” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ramón Invarato

I design and build data platforms and intelligent systems, and I enjoy sharing that experience here.

You may also like

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.