Create a function write_line(file, output) that writes the contents of the string output to file. Be sure to add a newline character "\n" when you write output to the file.
Write a function write_line(file, output) that writes the contents of the string output to file. Be sure to add a newline character "\n" when you write output to the file. Also make sure that you close the file after writing to it.
Create a function read_n_lines(file, n) that reads the first n lines from file and returns them in a list. Be sure to remove the end of line character from each line before you add it to the list and close the file after you have read the lines.
def read_n_lines(file, n):
---
out = []
f = open(file)
---
for i in range(n):
---
for i in range(n+1): #paired
---
line = f.readline()
---
out.append(line.rstrip())
---
out.append(line) #paired
---
f.close()
---
return out
Write a function read_n_lines(file, n) that reads the first n lines from file and returns them in a list. Be sure to remove the end of line character from each line before you add it to the list and close the file after you have read the lines
Create a function count_words(file) that returns the number of words in the passed file. First create a variable num and initialize it. Then open the file. Loop reading a line from the file. Break the line at spaces and add the length of the resulting list to num. After you have read all the lines, close the file and return num.
def count_words(file):
---
num = 0
---
file_obj = open(file, "r")
---
for line in file_obj:
---
for line in "file_obj": #paired
---
words = line.split(" ")
---
words = line.break(" ") #paired
---
num = num + len(words)
---
num = num + words #paired
---
file_obj.close()
---
return(num)
Write a function count_words(file) that returns the number of words in the passed file. First create a variable num and initialize it. Then open the file. Loop reading a line from the file. Split the line at spaces and add the length of the resulting list to num. After you have read all the lines, close the file and return num.
Create a function count_starting_with(file, str) that returns the number of lines in the passed file that start with the characters in str. Be sure to close the file.
def count_starting_with(file, str):
---
num = 0
---
file_obj = open(file)
---
for line in file_obj:
---
if line.startswith(str):
---
if line.startsWith(str): #paired
---
num += 1
---
file_obj.close()
---
file.close() #paired
---
return(num)
Create a function count_starting_with(file, str) that returns the number of lines in the passed file that start with the characters in str. Be sure to close the file.
def write_squares(file)
---
outfile = open(file, "w")
---
for number in range(1, 11):
---
for number in range(1, 10): #paired
---
square = number * number
---
square = number * 2 #paired
---
outfile.write(str(square) + "\n")
---
outfile.write(str(square)) #paired
---
outfile.close()
---
close(outfile) #paired