If you use lists to store the data from your program, there is a neat function called pickle() that can be used to save the entire list rather than using loops The example below saves a list to a file and then reloads it The data is not stored as text though, so you can’t […]
Category: Uncategorized
A really important thing you will need to be able to do is read and write text files with variable numbers of lines Because we don’t know how many lines there will be, we tend to read text files into a list, make any changes, and then write the whole list back over the file […]
A really important thing you will need to be able to do is read and write text files with variable numbers of lines Because we don’t know how many lines there will be, we tend to read text files into a list, make any changes, and then write the whole list back over the file […]
Writing delimited files is easy. You simply combine the items for each line, separated by a comma, with a \n on the end and then write it to the file Tasks Type in the code above and make sure it works Modify the program to accept user input Save and label your code Extension Task […]
Sometimes, you need to read in files from other programs containing information such as student records or address details. We use files known as comma separated values files They are simply text files with a different extension (.csv) Each line will have multiple items on it, each usually separated by a comma. It might look […]
When writing to files, you can either overwrite the information in the file (w), or add to it as you go along (a). This is set as shown below. Choose one or the other, not both There is a function called writeline() that you might assume is the opposite of readline(). Using this is slightly […]
This example uses a file savedfile.txt with a list of names and reads in each line at a time Each time readline() is called, the file advances by one line This is useful if you know exactly where you saved specific items in the file, and you know how many lines there are in total […]
The simplest thing you might need to do with files is to load or save a single item of data The example below uses a text file called savedfile.txt to read/write a number The program will produce an error if it does not exist so create it first (in the same directory where your program […]
Reading and writing files is fairly simple if you know what you want to load and save There are simple techniques for reading and writing individual items of data to and from a text file, a line at a time or all at once Reading multiple lines is usually done with a for loop and […]
You can chop up strings and check individual parts when the input may be multiple words or numbers separated by spaces or particular characters such as full names or dates In the last example you split strings into an array but you can access individual bits of them by number to save time Remember to […]