Python Interview Questions
Python
Web DevelopmentFrontendBackendData ScienceQuestion 20
How do you read from and write to a file in Python?
Answer:
Reading from and writing to files in Python is a common task that can be accomplished using built-in functions. Below are the methods for reading from and writing to files, along with examples.
Opening a File
Before you can read from or write to a file, you need to open it using the open()
function. The open()
function returns a file object, and it takes two primary arguments: the filename and the mode.
- Modes:
'r'
: Read (default mode) - Opens the file for reading.'w'
: Write - Opens the file for writing (creates a new file or truncates an existing file).'a'
: Append - Opens the file for writing (creates a new file or appends to an existing file).'b'
: Binary mode - Used with other modes to read or write binary files.'+'
: Update mode - Allows reading and writing (used with other modes like'r+'
,'w+'
,'a+'
).
Writing to a File
To write to a file, you open it in 'w'
(write) or 'a'
(append) mode. Here's an example:
Example:
# Open a file in write mode
with open('example.txt', 'w') as file:
file.write('Hello, World!\n')
file.write('This is a test file.\n')
# Open a file in append mode
with open('example.txt', 'a') as file:
file.write('Appending a new line.\n')
In this example:
- The file
example.txt
is opened in write mode. If it doesn't exist, it will be created. If it exists, its contents will be overwritten. - The
write()
method is used to write text to the file. - The
with
statement ensures that the file is properly closed after writing.
Reading from a File
To read from a file, you open it in 'r'
(read) mode. Here are different ways to read from a file:
Example 1: Reading the Entire File
# Open a file in read mode
with open('example.txt', 'r') as file:
content = file.read()
print(content)
In this example:
- The file
example.txt
is opened in read mode. - The
read()
method reads the entire content of the file and stores it in the variablecontent
.
Example 2: Reading Line by Line
# Open a file in read mode
with open('example.txt', 'r') as file:
for line in file:
print(line, end='')
In this example:
- The file
example.txt
is opened in read mode. - The file object is iterated over line by line, and each line is printed.
Example 3: Reading Specific Number of Characters
# Open a file in read mode
with open('example.txt', 'r') as file:
content = file.read(5) # Read the first 5 characters
print(content)
In this example:
- The
read(5)
method reads the first 5 characters of the file.
Example 4: Reading All Lines into a List
# Open a file in read mode
with open('example.txt', 'r') as file:
lines = file.readlines()
for line in lines:
print(line, end='')
In this example:
- The
readlines()
method reads all lines of the file into a list.
Summary
- Opening a File: Use the
open()
function with the appropriate mode. - Writing to a File: Use
write()
method to write text to a file. Use'w'
mode to overwrite and'a'
mode to append. - Reading from a File: Use
read()
,readline()
, orreadlines()
methods to read the file's content. Use'r'
mode to read. - Closing a File: Use the
with
statement to ensure the file is properly closed after reading or writing.
By using these methods, you can effectively read from and write to files in Python, handling both text and binary data as needed.