Why use files?
An Introduction from Craig n Dave
Watch the following video to get an idea of how files work before reading on.
Exclusive Access
When working with files it's important that you and only have access. If the file is already open somewhere else your program will give an error or crash ( you can "catch" these errors to prevent crashing). To ensure that you have exclusive access you have to open the file first, do something, then close the file.
|
Saving Data into a Text File
To save data into a file the code follows the same steps. Open the file, do something, close the file. The code below opens the file, saves the sentence " here is more text" and then closes the file:
f = open("MyFile.txt", "w") #use 'w' to write data. It will replace existing data. f.write("here is more text") f.write("this text will be saved and with a new line after"+ "\n") f.write("this text will be on a new line") f.close() |
Adding More Data ( appending) to an Existing File
To add even more data into an existing file, the code follows the same steps, but use the code 'a' ( to append) instead of 'w' ( to write):
f = open("MyFile.txt", "a") #use 'a' to append data. It will add to existing data.
f.write("here is more text")
f.close()
f = open("MyFile.txt", "a") #use 'a' to append data. It will add to existing data.
f.write("here is more text")
f.close()
Reading Data from a Text FileOnce you have data saved into a file you can retrieve it.
1. First you need to open the file: f = open("MyText.txt") 2. Once you have successfully opened the file. You can retrieve and store data in a string line by line: myMessage = f.readline() print(myMessage) Try now opening your text file, adding more text on different lines. You can retrieve all of the code together. f = open("c:\\Users\\Kamini.Mahendran\\Downloads\\MyText.txt") for str in f: print(str) |
Displaying Each Line from a File
You can also check through each line retrieved from your file with the following code:
f = open("MyData.txt", 'r')
for line in f: #go through each line retrieved from the file one by one.
print(line)
for line in f: #go through each line retrieved from the file one by one.
print(line)
Trouble ShootingDid it work? If not:
|
I can save to a text file, so why do I need to save to a CSV file?
If you open the same file in Microsoft Excel, you'll see the data organised into a table. The commas are used as delimiters to separate the data into cells.
Creating a CSV File using Microsoft Excel.
You can easily create a CSV file by typing data into a spreadsheet in Excel and then saving it as a CSV file. When you open the same file in notepad, you will see the commas separating the data.
Saving data to a CSV file
Open a .csv file. If you don't have one, it will be created. Then save your data with the write command. Put a comma after to go to a new cell.
MyFile = open("MyData.csv", 'w')
MyFile.write("hello"+ ",") # put a comma after each piece of text to save it to a new cell.
MyFile.close()
MyFile = open("MyData.csv", 'w')
MyFile.write("hello"+ ",") # put a comma after each piece of text to save it to a new cell.
MyFile.close()
Retrieving data from a CSV file
Open the file to read:
MyFile = open("MyData.csv", 'r')
Go through each line retrieved from the file one by one. Then use the split command to separate each line by the comma. The data will save into an array:
for line in MyFile:
words = line.split(',') # split at the comma into a list of words
for word in words: # go through each word one at a time.
print( word)
MyFile = open("MyData.csv", 'r')
Go through each line retrieved from the file one by one. Then use the split command to separate each line by the comma. The data will save into an array:
for line in MyFile:
words = line.split(',') # split at the comma into a list of words
for word in words: # go through each word one at a time.
print( word)
Other ways to Open Files
You can also use the following codes to open your file:
( Thanks to Lorenzo for finding these )