You are on page 1of 2

1.

CSV: Reading and writing data in CSV format may be slower and less efficient than binary files because
data conversion (between text and numeric values) is often required.

Binary File: Binary files are more efficient for reading and writing data since they store it in a format that
can be directly used by the computer's memory.

2. seek(offset, whence): The seek() function is used to change the current file position (cursor) within a
file. It takes two arguments:

offset: This is the number of bytes to move the file cursor. A positive value moves the cursor forward,
and a negative value moves it backward.

whence (optional, defaults to 0): It specifies the reference point for the offset.

tell() Function:

tell(): The tell() function is used to retrieve the current file position (cursor) within the file. It returns an
integer representing the current offset in bytes from the beginning of the file.

3. To count the number of occurrences of the word "is" in a text file, you can use the following Python
code:

file_path = "your_text_file.txt" # Replace with the path to your text file

count = 0

with open(file_path, "r") as file:

# Read the file line by line

for line in file:

# Split each line into words

words = line.split()

for word in words:

if word.lower() == "is": # Use lower() for case-insensitive matching

count += 1

print(f"The word 'is' appears {count} times in the file '{file_path}'.")

4. import csv

# Define the data you want to add to the CSV file


data = [

["Name", "Age", "City"],

["Alice", 28, "New York"],

["Bob", 35, "Los Angeles"],

["Charlie", 22, "Chicago"],

csv_file = "example.csv"

# Write the data to the CSV file

with open(csv_file, mode='w', newline='') as file:

writer = csv.writer(file)

writer.writerows(data)

print(f"Data has been written to '{csv_file}'")

5. "w" Mode:

"w" stands for write mode.

When a file is opened in "w" mode, it is created if it does not exist, and if it does exist, its content is
truncated, meaning it is cleared.

a" Mode:

"a" stands for append mode.

When a file is opened in "a" mode, it is created if it does not exist, but if it does exist, the file's content is
not cleared. Instead, data is appended to the end of the file.

"r+" Mode:

"r+" stands for read and write mode.

When a file is opened in "r+" mode, you can both read from and write to the file.

A CSV reader is a tool or module in Python that allows you to read data from CSV files efficiently.

You might also like