with Statement
● Advantages of with:
Automatically closes the file when the block of code is exited, even if an error
occurs.
Cleaner and less prone to errors than manually using close().
Example:
with open("[Link]", "w") as file:
[Link]("Hello with with!")
File Modes in Python:
1. r (Read mode):
with open("[Link]", "r") as file:
content = [Link]()
print(content)
2. r+ (Read and Write mode):
with open("[Link]", "r+") as file:
content = [Link]()
print(content)
[Link]("\nNew content added.")
3. w (Write mode):
with open("[Link]", "w") as file:
[Link]("New content.")
4. w+ (Read and Write mode):
with open("[Link]", "w+") as file:
[Link]("New content.")
[Link](0) # Move to the beginning of the file
content = [Link]()
print(content)
5. a (Append mode):
with open("[Link]", "a") as file:
[Link]("\nAppended content.")
6. a+ (Read and Append mode):
with open("[Link]", "a+") as file:
[Link]("\nAppended content.")
[Link](0)
content = [Link]()
print(content)
Key points:
r and r+ modes require the file to exist.
w and w+ modes truncate the file if it exists, or create a new file if it doesn't.
a and a+ modes append to the file if it exists, or create a new file if it doesn't.
seek(0) is used to move the file pointer to the beginning of the file for reading.
Write Operation (Writing to a File)
write()
1. Writes a single string to a file.
2. Syntax: [Link](string)
3. Newline characters: Does not automatically add newline characters; you need to include \n in the
string.
writelines()
1. Writes multiple strings to a file.
2. Syntax: [Link](iterable_of_strings)
3. Newline characters: Does not automatically add newline characters; you need to include \n in each
string.
Key differences
- write() takes a single string, while writelines() takes an iterable of strings (like a list or tuple).
- Neither write() nor writelines() automatically adds newline characters; you need to include them in the
strings.
Example
with open("[Link]", "w") as f:
[Link]("Hello, World!\n") # Write a single string
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
[Link](lines) # Write multiple strings
seek() - used with different arguments to move the file pointer to various positions in the file.
Syntax for seek() is: [Link](offset, whence=0)
offset: The number of bytes to move the file pointer.
whence: The reference position from the offset(default is 0, which means the beginning of the file).
0 : Beginning of the file. (TEXT FILES WE MOSTLY USED WHENCE-0)
1 : Current file pointer position.
2 : End of the file.
seek(0,0): Moves the file pointer to the beginning of the file.
seek(5,0): Moves the file pointer to the 6th byte (index 5) in the file.
seek(0, 2): Moves the file pointer to the end of the file.
Some examples of seek():
EX.1
with open("[Link]", "w+") as file:
[Link]("New content.")
[Link](0) # Move to the beginning of the file
content = [Link]()
print(content)
EX.2
with open("[Link]", "r+") as f:
[Link](5) # Move to the 6th byte
data = [Link]()
print(data)
[Link](0, 2) # Move to the end of the file
[Link]("Appended text")
Ques: Write a Python program to read the contents of a file named "[Link]" and count the total
number of vowels (both lowercase and uppercase) present in the file. Display the file contents and the
vowel count.