Watch the videos and take notes, pay attention to methods of writing data to an external file.
Run and walk through the example (in code window).
Read the task and write code to solve.
Make a note of successful code in your book.
Answer questions in your book.
Watch the video and create notes in order to be able able to apply the knowledge given.
Modes for writing files
'w' : use for writing
'x' : use for creating and writing to a new file
'a' : use for appending to a file
'r+' : use for reading and writing to the same file
String data to an external file
# writing string data to an external file
textFile = open("simplefile.txt","w") # open simplefile.txt in write mode
textFile.write("You can only write strings to an external file\n") # writes the literal string to the file and adds \n for to move to a new line
text = "You can write strings that are stored in variables or arrays to an external file\n" # assign string value to variable text
textFile.write(text) # write text variable to file
textFile.close() # close open file
print("No output will appear on screen, all output will appear in simplefile.txt (Tab at top of window)")
Array of strings to external file (format: one line separated by commas)
# writing data to an external file (string data from an array)
animals = ["Dog","Cat","Bird","Frog","Hedgehog","Fish","Snakes","Spiders"] # Array of integer values
animalFile = open("animals.txt","w") # uses the name animalsFile to represent the open file animals.txt in write mode
for count in range(0,len(animals),1): # for loop to access each array element one at at time
text = animals[count]+"\n" # creates and formats string output "," or commas "\n" for newline
animalFile.write(text) # writes the str value held in the variable text to the open file
animalFile.close() # closes the file
print("No output will appear on screen, all output will appear in animals.txt (Tab at top of window)")
print("This program formats the data in the file by putting new data on newlines")
Array of integers written to external file (format: each value written to new line "\n")
# writing data to an external file (integer data from an array)
scores = [23,456,8,64,34,7899,543,3457,9909,54,3456,945,3,32323,454] # Array of integer values
scoresFile = open("scores.txt","w") # uses the name scoresFile to represent the open file scores.txt in write mode
for count in range(0,len(scores),1): # for loop to access each array element one at at time
text = str(scores[count])+"," # creates and formats string output "," or commas "\n" for newline
scoresFile.write(text) # writes the str value held in the variable text to the open file
scoresFile.close() # closes the file
print("No output will appear on screen, all output will appear in scores.txt (Tab at top of window)")
print("This program formats the data in the file by putting commas between each value on the same line")
Apply your knowledge of writing data from an external file. Using the given array of floats write a program that will output them in series to an external file. When writing the data to the file it should be cast as string data and then each value when written should be separated by commas "," and only located on one line in the file.
decimals = [1.3, 2.1, 1.3, 254.1, 24.5, 0.1, 5.0, 6.5, 34.3, 99.2, 89.4, 34.2, 4.1, 34.5]
Use decimals array
open file in write mode
access each float value (data element) one at a time
construct string variable (cast float as string and concatenate "," )
write string variable to file
close file
decimals = [1.3, 2.1, 1.3, 254.1, 24.5, 0.1, 5.0, 6.5, 34.3, 99.2, 89.4, 34.2, 4.1, 34.5] # Array of float values
decimalsFile = open("decimals.txt","w")
for count in range(0,len(decimals),1):
text = str(decimals[count])+","
decimalsFile.write(text)
decimalsFile.close()
print("No output will appear on screen, all output will appear in decimals.txt (Tab at top of window)")
State the only data type able to be written to an external file.
Describe the purpose of each part of the following code:
text = str(45)+","
Describe the purpose of each part of the following code:
text = str(45)+"\n"
Referencing the example above state why might you format your string and create a text variable before writting the value to a file.
If data is held in an array describe why might you use a loop & length command when writting the data to a file.
A program reads data into an array when started (saved data) then edits, adds and removes data from the array. Describe why it is important when writing data back to the data file to ensure that the data is written in the same format.
State the only data type able to be written to an external file.
Only string data can be written to an external file.
Describe the purpose of each part of the following code:
text = str(45)+","
formatted data in text variable = cast to string value "45" then concatenate "," to separate
Describe the purpose of each part of the following code:
text = str(45)+"\n"
formatted data in text variable = cast to string value "45" then concatenate "\n" for a new line
Referencing the example above state why might you format your string and create a text variable before writing the value to a file.
A text variable is used to format the data as the write () command will only accept one argument / data item at a time.
If data is held in an array describe why might you use a loop & length command when writing the data to a file.
If data is held in an array then you would use the length command to find out the number of items in the array then create a for loop to move through the array one element at a time in order to format and write them to a file.
A program reads data into an array when started (saved data) then edits, adds and removes data from the array. Describe why it is important when writing data back to the data file to ensure that the data is written in the same format.
If you read data and then attempt to write the data back to the file using a different format then the next time the program runs the data will be read incorrectly. So the reading format should match the writing format to allow the program to save and reload data correctly.
Example: imagine when you save & load your progress in your favorite game. The next time you load your game if the developer didn't plan how data would be stored externally the data couldn't used correctly. If it was correctly read but then written back when you save in a different format then it could never be accessible again and you progress would be lost.