0% found this document useful (0 votes)
46 views28 pages

Writing Files

The code defines a function to write a list of text items to a text file. It opens the file in write mode, loops through the list, writes each item plus an end of line character to the file, then closes the file. This allows easy writing of lists or other structured data to text files in a consistent way.

Uploaded by

Jake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views28 pages

Writing Files

The code defines a function to write a list of text items to a text file. It opens the file in write mode, loops through the list, writes each item plus an end of line character to the file, then closes the file. This allows easy writing of lists or other structured data to text files in a consistent way.

Uploaded by

Jake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

In pairs, predict what the following code does

1 def write_text_file():
2 file=open("days.txt",’w’)
3 file.write("Monday\n")
4
5 file.write("Tuesday\n")
6 file.write("Wednesday\n")
file.close()
7
write_text_file()
Writing to text files
Learning objectives
Write programs that write data to text files
Write programs that append data to text files
Storing data
Sometimes we need to store data and information in our programs.
To do this we can use files that allow us to save work between
sessions.

There are all sorts of file types for storing different media such as
music, pictures, movies and documents

We are going to learn to write to text files


Writing to files

To write to files there are 3 steps:


Open the file for writing
Write to the file
Close the file
Download the code, run and modify
1 def write_text_file(): Does the code do as you expected?

2 file=open("days.txt",’w’) Download the code from write-days.py


3 file.write("Monday\n")
4 Add in the remaining days of the week
5 file.write("Tuesday\n")
6 file.write("Wednesday\n") What happens if you remove the \n on lines 3-5.
file.close()
7 What does “w” do on line 2?
write_text_file()
What is the name of the file?

What happens if the file exists?

What happens if the file does not exist?


Open file
Whatever we are doing to a file whether we are reading, writing or adding to or modifying a file we
first need to open it using:

open(filename,access_mode)

There are a range of access modes depending on what we want to do to the file, the principal ones are
given below:

file=open(“file.txt”,’r’) Opens a file for reading only, there will be an error if the
file does not exist
file=open(“file.txt”,’w’) Opens a file for writing only, create the file if it does not
exist, and overwrites the file if it does already exist
file=open(“file.txt”,’a’) Append to the end of a file, creates the file if it does not
already exist
Open the file for writing
Name of file
1 def write_text_file():
2 file=open("days.txt",’w’)
3 file.write("Monday\n")
4
5 file.write("Tuesday\n")
6 file.write("Wednesday\n")
file.close()
7
write_text_file()
Open the file for writing
1 def write_text_file():  We need to specify the
access mode to be ‘w’.
2 file=open("days.txt",’w’)
3 file.write("Monday\n")  The ‘w’ means open a file
4 for writing only.
5 file.write("Tuesday\n")
6 file.write("Wednesday\n")
file.close()  Warning: If the file already
7 exists everything will be
write_text_file() overwritten.

 If the file does not exist,


one will be created.
Writing to the file
1 def write_text_file():  Now that we have opened the file we can
write to it.
2 file=open("days.txt",’w’)
3 file.write("Monday\n")
4  We can add content to a file using the write
5 file.write("Tuesday\n")
file.write("Wednesday\n") method.
6
file.close()
7  Write allows us to add a string to the file.

write_text_file()
 At the end of each string we include “\n” so
that each day is written to a separate line. IN
this example each day of the week will be on
on a separate line.
Writing to the file
1 def write_text_file():  Now that we have opened the file we can
write to it.
2 f1=open("days.txt",’w’)
3 f2=open(“months.txt",’w’)
4  We can add content to a file using the write
5 f1.write("Monday\n")
f1.write("Tuesday\n") method.
6
f1.write("Wednesday\n")
7 f2.write(“January\n")  Write allows us to add a string to the file.
f1.close()
f2.close()
 At the end of each string we include “\n” so
write_text_file() that each day is written to a separate line. IN
this example each day of the week will be on
on a separate line.
Close file
1 def write_text_file(): Once we have written to
2 file=open("days.txt",’w’)
the file we need to close
3 file.write("Monday\n") the file using the close
4
5 file.write("Tuesday\n") method
6 file.write("Wednesday\n")
file.close()
7 You can view the file in a
write_text_file() text editor (eg Notepad) to
check that it has been
written correctly.
Modify the code
1 def write_text_file(): Change the access specifier
2 “w” to “a”. Run the
file=open("days.txt",’w’)
3 file.write("Monday\n") code. What happens?
4
5 file.write("Tuesday\n")
6 file.write("Wednesday\n")
file.close()
Add in the remaining days of
7 the week.
write_text_file()
Change the name of the file
Procedure for writing a list to a file
1 def write_text_file(data,file,end=“”):
2 f=open(file,“w") This is a general procedure
3
for i in data: for writing a list to a text
4
5
text=i+end file. It can be used in many
6
f.write(text) situations.
f.close()
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Writing to a file

 Access mode “” means


1 def write_text_file(data,file,end=“”):
write to the the file
2 f=open(file,“w")
3  Create a new file if one
for i in data:
4 does not exist.
text=i+end
5
f.write(text)  Overwrites the file if it does
6
f.close() already exist
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Writing to a file
1 def write_text_file(data,file,end=“”):
2 f=open(file,“w")
3
for i in data: File to append to. This is the parameter
4
text=i+end name.
5
f.write(text)
6 In this example the file is called
f.close() “greeting.txt”
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Writing to a file

1 def write_text_file(data,file,end=“”):
2 f=open(file,“w")
3 List to write to file
for i in data:
4
text=i+end
5
f.write(text)
6
f.close()
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Procedure for writing a list to a file
1 def write_text_file(data,file,end=“”):
2 f=open(file,“w") Go through each item in
3
for i in data: the list
4
text=i+end
5
f.write(text)
6
f.close()
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Writing to a file
1 def write_text_file(data,file,end=“”):
2 f=open(file,“w")
3 Adds end of line character if needed
for i in data: to append onto the end of the line so
4
text=i+end that the text is appended onto the
5 next line in the file if end=“\n”
f.write(text)
6
f.close()
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Writing to a file
1 def write_text_file(data,file,end=“”):
2 f=open(file,“w")
3 Write list item to file
for i in data:
4
text=i+end
5
f.write(text)
6
f.close()
7
8 data=[“Mon”,”Tue”,”Wed”]
write_text_file(data,“days.txt“,”\n”)
Modify the code
1 def write_text_file(data,file,end=“”): Change the access
2
3
f=open(file,“w")
specifier “w” to “a”. Run
for i in data:
4 the code. What happens?
text=i+end
5
f.write(text)
6
f.close() Change the name of the
7 output file to
8 data=[“Mon”,”Tue”,”Wed”]
months.txt
write_text_file(data,“days.txt“,”\n”)
Use the months of the
year for the data list
Alternative procedure for appending text to files

1 def append_text(text,file,end=“”):
2
To add a line to a file
text=text+end
3 that already exists, we
f=open(file,"a")
4
f.write(text)
can use a generalised
5
f.close() routine
7
8 text=“Hello”
9 append_text(text,“greeting.txt“,”\n”)
10 text=“World”
append_text(text,“greeting.txt“,”\n”)
Appending lines

1 def append_text(text,file,end=“”):
2 text=text+end Adds end of line character if needed
3 to append onto the end of the line so
f=open(file,"a")
4 that the text is appended onto the
f.write(text) next line in the file if end=“\n”
5
f.close()

7
8 text=“Hello”
9 append_text(text,“greeting.txt“,”\n”)
10 text=“World”
append_text(text,“greeting.txt“,”\n”)
Appending lines

1 def append_text(text,file,end=“”):
2  Access mode “a” means
text=text+end
3 append to the end of a file
f=open(file,"a") if it exists.
4
f.write(text)
5
f.close()  Create a new file if one
does not exist.
7
8 text=“Hello”
9 append_text(text,“greeting.txt“,”\n”)
10 text=“World”
append_text(text,“greeting.txt“,”\n”)
Appending lines

1 def append_text(text,file,end=“”):
2 text=text+end
3 File to append to. This is the parameter
f=open(file,"a")
4 name.
f.write(text)
5
f.close() In this example the file is called
“greeting.txt”
7
8 text=“Hello”
9 append_text(text,“greeting.txt“,”\n”)
10 text=“World”
append_text(text,“greeting.txt“,”\n”)
Appending lines

1 def append_text(text,file,end=“”):
2 text=text+end
3 Text to append to file
f=open(file,"a")
4
f.write(text)
5
f.close()

7
8 text=“Hello”
9 append_text(text,“greeting.txt“,”\n”)
10 text=“World”
append_text(text,“greeting.txt“,”\n”)
Make

Write a program that generates a shopping list


 Asks the user for 5 items on a shopping
 The items are entered in one at a time
 The items are added to a file
 Each item is on a different line
 Use the append_text routine or
write_text_file routine

You might also like