0% found this document useful (0 votes)
12 views5 pages

Files

The document outlines various file handling functions in Python, detailing their purposes and usage. It includes methods for opening files in different modes (reading, writing, appending), reading lines from files, writing strings or lists of strings to files, and closing files to free resources. Examples are provided for each function to illustrate their application.

Uploaded by

hello Libby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Files

The document outlines various file handling functions in Python, detailing their purposes and usage. It includes methods for opening files in different modes (reading, writing, appending), reading lines from files, writing strings or lists of strings to files, and closing files to free resources. Examples are provided for each function to illustrate their application.

Uploaded by

hello Libby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMPUTER SCIENCE

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") # Opens "example.txt" for reading

• 2. for <line> in <fileid>


• Purpose: Reads every line from the file, one at a time.
• This loop allows you to iterate through the file, reading each line.
• <line> represents one line of text in the file.
• The file is read line by line, making it memory efficient.
• 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

• ['First line\n', 'Second line\n', 'Third line\n']


4. <aline> = <fileid>.readline()
Purpose: Reads one line from the file.
•This function reads just one line from the file each time it is called.
•It returns an empty string "" when it reaches the end of the file.
Example:
python

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

If you call readline() again, it will return the next line.


• 5. <fileid> = open(<filename>, "w")
• Purpose: Opens a file for writing.
• "w" stands for write mode.
• If the file exists, it overwrites the content.
• If the file does not exist, it creates a new file.
• Example:
• python

• 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()

• Explanation: This writes the text "This is a single line." to example.txt.


• 9. <fileid>.close()
• Purpose: Closes the file.
• This function is important because it frees up system resources and ensures that all data is written to the file.
• Always call close() after reading or writing to a file to prevent data loss.
• Example:
• python

• 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

You might also like