# write a function # to read students.txt file # for each line, add student's email address, separated by comma # using the following format # name,email # then, write the students' information (name,address) to # another file named studentinfo.txt # # Download: students.txt def inclass(): infile = open('students.txt','r') # if the file does not exist, create one and set it to be writable outfile = open('studentinfo.txt', 'w') line = infile.readline() string1 = '' while line != '': name = line.rstrip('\n') string1 = string1 + name + ',' + name + '@virginia.edu' + '\n' line = infile.readline() # what happen if this line is commented out print(string1) outfile.write(string1) # close the file infile.close() outfile.close() def write_file(): # open a file named students.txt outfile = open('students.txt', 'w') # write the names of three students to the file outfile.write('John\n') outfile.write('Jack\n') outfile.write('Jane\n') # close the file outfile.close() def append_file(): # open a file named students.txt outfile = open('students.txt', 'a') # append another student's name to the file outfile.write('Mary\n') # close the file outfile.close() def readline_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read 2 lines from the file line1 = infile.readline() line2 = infile.readline() print("---- readline_file() ----") # notice the differences how the following statements print print(line1) print(line2) print(line1, line2) # now, let's try formatting the lines # by stripping the \n from each string line1 = line1.strip('\n') # remove from left and right line2 = line2.rstrip('\n') # remove from right end print(line1) print(line2) print(line1, line2) # close the file infile.close() def readline_file_with_loop(): # open a file named students.txt to be read infile = open('students.txt', 'r') line = infile.readline() print("---- readline_file_with_loops ----") while line != '': print(line.rstrip('\n')) line = infile.readline() # what happen if this line is commented out # close the file infile.close() def read_file(): # open a file named students.txt to be read infile = open('students.txt', 'r') # read the file's contents file_contents = infile.read() print("---- read_file() ----") print(file_contents) # close the file infile.close() write_file() append_file() readline_file() readline_file_with_loop() read_file() inclass() # Q: Where is the student.txt file ? What does it look like?