Python Write code that does the following: opens an output file number list.txt,
ID: 3848488 • Letter: P
Question
Python
Write code that does the following: opens an output file number list.txt, uses a loop to write the numbers 1 through 100 to the file and then closes the file. Write code that does the following: opens the number list.txt file that was created by the code you wrote in question 1, reads all of the numbers from the file and displays them, and then closes the file. Modify the code that you wrote in question 2 so it adds all of the numbers read from the file and displays their total Write code that opens an output file with the filename number list.txt, but does not erase the file's contents if it already exists. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student's name, and (2) the student's score for the final exam. Write code that deletes the record containing "John Perz" as the student name. A file exists on the disk named students txt. The file contains several records, and each record contains two fields: (1) the student's name, and (2) the student's score for the final exam. Write code that changes Julie Milan's score to 100.Explanation / Answer
1)
Code:
#!/usr/local/bin/python3
# script name: code_1.py
def main():
# opening the file number_list.txt in writing mode
fout = open("number_list.txt", "w")
# iterating through 1 to 100 numbers
for num in range(1, 101):
# writing to file
print(num, file=fout)
# closing the file
fout.close()
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 code_1.py
Unix Terminal> ls -lthr number_list.txt
-rw-r--r-- 1 bonkv ANTDomain Users 292B Jun 12 21:11 number_list.txt
Unix Terminal> head number_list.txt
1
2
3
4
5
6
7
8
9
10
Unix Terminal> tail number_list.txt
91
92
93
94
95
96
97
98
99
100
Unix Terminal>
2)
Code:
#!/usr/local/bin/python3
# script name: code_2.py
def main():
# opening the file number_list.txt for reading
with open("number_list.txt") as fp:
# fetching each line from file
for each_line in fp:
print(each_line)
if __name__=='__main__':
Execution and output:
Unix Terminal> python3 code_2.py|head
1
2
3
4
5
3)
Code:
#!/usr/local/bin/python3
# script name: code_2.py
def main():
total = 0
# opening the file number_list.txt for reading
with open("number_list.txt") as fp:
# fetching each line from file
for each_line in fp:
each_line = int(each_line.strip())
# summing each number
total += each_line
print(" Sum of numbers from the file number_list.txt: "+str(total))
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python3 code_3.py
Sum of numbers from the file number_list.txt: 5050
4)
Code:
#!/usr/local/bin/python3
# script name: code_4.py
def main():
# opening the file number_list.txt in append mode so it will not write if it exists
fout = open("number_list.txt", "a")
# added a line to end of the file
fout.write('I have added a line to test')
fout.close()
if __name__=='__main__':
main()
Execution and output:
Unix Terminal> python code_4.py
Unix Terminal> tail number_list.txt
92
93
94
95
96
97
98
99
100
I have added a line to test
Unix Terminal>
5)
Code:
#!/usr/local/bin/python3
# script name: del_student.py
def main():
stud = {}
# opening the file students.txt in reading mode
with open("students.txt") as fp:
print(" Printing the file before deleting student John Perz")
for each_line in fp:
print(each_line)
# fetching student and score from each line
student, score = each_line.split(',')
# creating a dictionary stud with key as student and score as value
stud[student] = score
# deleting the student John Perz
del stud['John Perz']
fp.close()
# opening the file in write mode
fp = open("students.txt", "w")
# writing dictionary which is file contents after deleting student John Perz
for each_stud in stud:
line = each_stud + "," + str(stud[each_stud])
print(line, file=fp)
if __name__=='__main__':
main()
Execution and output:
displaying the file before deleting
Unix Terminal> cat students.txt
krishna,98
Ravi,100
Robert Lafore,95
Henry Luther,98
John Perz,99
Tannenbaum,84
Dennis Ritchie,100
script executed
Unix Terminal> python3 del_student.py
Printing the file before deleting student John Perz
krishna,98
Ravi,100
Robert Lafore,95
Henry Luther,98
John Perz,99
Tannenbaum,84
Dennis Ritchie,100
after script execution
Unix Terminal> cat students.txt
krishna,98
Ravi,100
Robert Lafore,95
Henry Luther,98
Tannenbaum,84
Dennis Ritchie,100
Unix Terminal>
6)
Code:
#!/usr/local/bin/python3
# script name: update_student.py
def main():
stud = {}
# opening the file students.txt in reading mode
with open("students.txt") as fp:
for each_line in fp:
# fetching student and score from each line
student, score = each_line.split(',')
# creating a dictionary stud with key as student and score as value
stud[student] = score
fp.close()
stud['Julie Milan'] = 100
# opening the file in write mode
fp = open("students.txt", "w")
# writing dictionary to file after updating Julie Milan score
for each_stud in stud:
line = each_stud + "," + str(stud[each_stud])
print(line, file=fp)
if __name__=='__main__':
main()
Execution and output:
file contents before script execution:-
Unix Terminal> cat students.txt
krishna,98
Ravi,100
Robert Lafore,95
Henry Luther,98
Tannenbaum,84
Dennis Ritchie,100
Julie Milan,89
script execution
Unix Terminal> python3 update_student.py
file contents updated after script execution
Unix Terminal> cat students.txt
krishna,98
Ravi,100
Robert Lafore,95
Henry Luther,98
Tannenbaum,84
Dennis Ritchie,100
Julie Milan,100
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.