CS1-FULL-FALL2021
CS1
Week 7
Sublists and Files
Maíra Marques Samary
Sub lists
• In Python it is possible to create a list using a
subset of the elements of another list, keeping
the order of the original elements.
• We call this a sub list.
• To create a sub list, we must indicate the range of
elements of the original list that we want to use.
• For this, we specify the indexes of the first element
and the last one (+1). Examples:
• list [0: 3] # considers the elements from 0 to 2
• list [: 3] # same as the previous one by not
specifying index on the left
• list [: 2] # consider the elements with index 0 and 1
• list [0:] # considers the elements from the first to the
last (all)list [:] # same as the previous
1980
File System
• In our computer there is a file system that organizes the
files in folders (or directories) in a hierarchical way.
• Every file system has a root folder.
• Sub folders are created from the root folder.
• You can create as many sub folders within folders as
you like.
• There can be several child folders within a root
folder.
File System
• A file always is inside a folder.
• A file inside a folder has a unique name.
• The name of a file in a folder can not
be repeated.
• But it can be repeated in different
folders.
File Systems
• The files can be binary or text.
• A binary file contains information that human
beings can not directly understand; we need a
program that decodes the information.
• Examples: Digital photographs, MP3 audio, MP4
videos, etc.
• A text file can be understood by humans.
• Examples: simple text file, Python code file.
In this course we will only work with text files.
!
Basic operations with files
Basic Operations
• Open file
• Read line
• Write line
• Close file
Open File
• Why we need to open a file?
• If we are going to read a file, it is necessary to
verify that the file exists and obtain a variable
that allows us to read from the file.
• If we are going to write in a file, it is
necessary to ask the operating system to
create the file in a certain folder, and then
obtain a variable that allows us to write.
Open File
• To open a file for reading we use the function
open() indicating the path of the file:
Python will assume
that the required file is
in the same folder as
the program that is
running.
Open File
In this case it is specified that the
file is in a folder called “texts”,
daughter of the one where the
Python program resides.
Open File
In this case it is specified that the
file is in a folder called “texts”,
under the parent folder of the one
where the Python program
resides, that is, the sister folder of
the folder where the program
resides.
Open File
• To open a file for writing we use the function open()
indicating the path of the file and a string with the writing
mode ’w’, or ”w”
Python will assume
that the required file is
in the same folder as
the program that is
running.
Read Line
• Reading line makes sense when the file we read is a text
file.
• When we are editing a text file manually and pressing the
enter key, a new line is generated (a line break character)
• We can assume that at the end of each line there is a line
break character :
• \n on macOS, Linux, and other Unix-derived systems
• \r \n if it is Windows
Read Line
• When we open a file, a cursor is created that we
use to advance through the file as we
read/write.
• We can read the following text line of a file with
the readline() function.
• This function returns a string with the line read
and advances the cursor to the beginning of the
next line.
Reading of two
consecutive lines in
independent
variables.
How to know with readline() if it has arrived at the
end of the file?
?
End of file check if the
string read has zero
length
It is possible to read all the lines of a file with a cycle.
!
Con un ciclo while
Con un ciclo for
The for automatically
read each line of the
file in the loop variable
line
Let’s write a program that prints itself on the screen (reading from
where it is stored).
!
El mismo programa usando un ciclo for
Important: After using a file to read or write,
You have to close it!
!
Write Text Files
• Writing to a text file requires opening
the file in write mode.
• When you open a file in write mode, an
empty file is created.
• If an existing file is opened in write
mode, all content is lost.
Write Text Files
• The basic operation to write a line to file is
write()
• The string that is saved in the next line
of the file is passed as a parameter.
• Manually add the line breaks \r \n or \n
to the end of each line.
Writing Example
What this program do?
Resultado
Some typical questions …
Can we read numbers from a file with readline()?
?
Integer numbers or decimal numbers?
?
Can an existing file be opened in writing mode and keep writing
from the end?
?
Append
• Lines can be added to a pre-existing file if
it is opened in append mode.
• For this, we use the ‘a’ character as the
second parameter of the open function.
• If the file that opens with append mode
does not exist, it is created empty,
automatically.
Can two files be opened and traversed at the same time?
?
Let’s try
Write a program that opens two files entered by the
user and then print the lines of both files alternately
!