Professional Documents
Culture Documents
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
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.
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
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