Files
Files
PAPER 2
Function/Method Purpose
open(<filename>, "r") Opens file for reading
Loops through and reads the file line by
for <line> in <fileid>
line
<fileid>.readlines() Reads all lines from the file into a list
<fileid>.readline() Reads a single line from the file
Opens file for writing, overwriting existing
open(<filename>, "w")
content
Opens file for appending (adds data at the
open(<filename>, "a")
end)
<fileid>.writelines(<structure>) Writes a list of strings to the file
<fileid>.write(<aString>) Writes a single string to the file
Closes the file to save data and free
<fileid>.close()
resources
• 1. <fileid> = open(<filename>, "r")
• Purpose: Opens a file for reading.
• "r" stands for read mode.
• This mode opens the file and allows you to read its contents.
• If the file doesn't exist, it will throw an error (FileNotFoundError).
• Example:
• python
• fileid = open("example.txt", "r") for line in fileid: print(line) # Prints each line in the file fileid.close()
• Explanation: This will print all lines in the file, one by one, without loading the entire file into memory at once.
• 3. <alist> = <fileid>.readlines()
• Purpose: Returns a list where each item is a line from the file.
• This function reads all lines from the file and returns them as a list.
• Each line from the file becomes one element in the list.
• It can be memory-intensive for large files, as the entire file is read into memory at once.
• Example:
• python
• fileid = open("example.txt", "r") alist = fileid.readlines() # Reads all lines into a list print(alist) # Prints the list of lines fileid.close()
• Output Example:
• css
fileid = open("example.txt", "r") aline = fileid.readline() # Reads one line from the file
print(aline) # Prints the first line fileid.close()
Output Example:
arduino
First line
• fileid = open("example.txt", "w") # Opens the file in write mode fileid.write("Hello, World!") # Writes text to the file fileid.close()
• Explanation: This will create or overwrite example.txt with the text "Hello, World!".
• 6. <fileid> = open(<filename>, "a")
• Purpose: Opens a file for appending.
• "a" stands for append mode.
• If the file exists, it adds new content at the end without deleting existing data.
• If the file doesn't exist, it creates a new file.
• Example:
• python
• fileid = open("example.txt", "a") # Opens the file in append mode fileid.write("\nAdding new line") # Adds text at the end fileid.close()
• Explanation: This will append the text "Adding new line" to example.txt.
• 7. <fileid>.writelines(<structure>)
• Purpose: Writes a list of strings to a file.
• writelines() takes a list of strings (called <structure>), and writes each string to the file.
• This function does not automatically add newline characters, so each string must include \n if you want to create new lines.
• Example:
• python
• fileid = open("example.txt", "w") lines = ["Line 1\n", "Line 2\n", "Line 3\n"] fileid.writelines(lines) # Writes each line from the list to the file fileid.close()
• Explanation: This will write the three lines to the file. If the \n were missing in the list, the content would be written on the same line.
• 8. <fileid>.write(<aString>)
• Purpose: Writes a single string to the file.
• This function writes a single string to the file.
• Unlike writelines(), it does not require a list, but the string must include newlines if you want to create line breaks.
• Example:
• python
• fileid = open("example.txt", "w") fileid.write("This is a single line.") # Writes a single string to the file fileid.close()
• fileid = open("example.txt", "w") fileid.write("Writing something...\n") fileid.close() # Closes the file after writing
• Explanation: Always call close() after file operations to ensure changes are saved and resources are freed.
• Summary of File Handling Functions