07-01 Files

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A file named data.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates two files, dataplus.txt and dataminus.txt, and copies all the lines of data.txt that have positive integers to dataplus.txt, and all the lines of data.txt that have negative integers to dataminus.txt. Zeros are not copied anywhere.

Solution 1 infile = open("data.txt", "r"); plus = open("dataplus.txt", "w"); minus = open("dataminus.txt", "w"); line = infile.readline( ) while line != " ": [Tab Key]if int(line)>0: [Tab Key][Tab Key]plus.write(line) [Tab Key]elif int(line)<0: [Tab Key][Tab Key]minus.write(line) [Tab Key]line = infile.readline( ) infile.close( ) plus.close( ) minus.close( ) Solution 2 infile = open("data.txt", "r"); plus = open("dataplus.txt", "w"); minus = open("dataminus.txt", "w") for line in infile: [Tab Key]if int(line)>0: [Tab Key][Tab Key]plus.write(line) [Tab Key]elif int(line)<0: [Tab Key][Tab Key]minus.write(line) infile.close( ) plus.close( ) minus.close( )

A file named data1.txt contains an unknown number of lines, each consisting of a single integer. Write some code that creates a file named data2.txt and copies all the lines of data1.txt to data1.txt.

Solution 1 infile = open("data1.txt", "r"); outfile = open("data2.txt", "w"); line = infile.readline( ) while line != " ": [Tab Key]outfile.write(line) [Tab Key]line = infile.readline( ) infile.close( ) outfile.close( ) Solution 2 infile = open("data1.txt", "r"); outfile = open("data2.txt", "w"); for line in infile: [Tab Key]outfile.write(line) infile.close( ) outfile.close( )

A file named numbers.txt contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file and stores the largest number read in a variable named maxvalue.

Solution 1 numfile = open("numbers.txt", "r"); maxvalue = 0 number = numfile.readline( ) while number != " ": [Tab Key]if int(number)>maxvalue: [Tab Key][Tab Key]maxvalue = int(number) [Tab Key]number = numfile.readline( ) numfile.close( ) Solution 2 numfile = open("numbers.txt", "r"); maxvalue = 0 for number in numfile: [Tab Key]if int(number)>maxvalue: [Tab Key][Tab Key]maxvalue = int(number) numfile.close( )

A file named numbers.txt contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file, ignoring those values that are not bigger than the maximum value read up to that point. The numbers that are NOT ignored are added, and their sum stored in a variable called runsum. For example, if the sequence of integers in the file were "9 7 5 18 13 2 22 16" the code would ignore: 7, 5, 13, 2 and 16; it would add 9, 18, and 22, storing their sum, 49, into runsum.

Solution 1 numfile = open("numbers.txt", "r"); maxvalue = 0 runsum = 0 number = numfile.readline( ) while number != " ": [Tab Key]if int(number)>maxvalue: [Tab Key][Tab Key]maxvalue = int(number) [Tab Key][Tab Key]runsum += int(number) [Tab Key]number = numfile.readline( ) numfile.close( ) Solution 2 numfile = open("numbers.txt", "r"); maxvalue = 0 runsum = 0 for number in numfile: [Tab Key]if int(number)>maxvalue: [Tab Key][Tab Key]maxvalue = int(number) [Tab Key][Tab Key]runsum += int(number) numfile.close( )

A file named numbers.txt contains an unknown number of lines, each consisting of a single integer. Write some code that computes the sum of all these integers, and stores this sum in a variable name sum.

Solution 1 numfile = open("numbers.txt", "r"); sum = 0 number = numfile.readline() while number != " ": [Tab Key]sum += int(number) [Tab Key]number = numfile.readline() numfile.close( ) Solution 2 numfile = open("numbers.txt", "r"); sum = 0 for number in numfile: [Tab Key]sum += int(number) numfile.close( )

Write a statement to open the file priceList.txt for writing.

Solution 1 open('priceList.txt', 'w') Solution 2 open('priceList.txt', 'a')

Assume there are four files named asiasales2009.txt, europesales2009.txt, africasales2009.txt, and latinamericasales2009.txt. Open these four files for writing and assign the resulting file objects to the variables asia, europe, africa, and latin.

asia = open('asiasales2009.txt', 'w') europe = open('europesales2009.txt', 'w') africa = open('africasales2009.txt', 'w') latin = open('latinamericasales2009.txt', 'w')

Two files named numbers1.txt and numbers2.txt both have an unknown number of lines, each line consisting of a single positive integer. Write some code that reads a line from one file and then a line from the other file. The two integers are multiplied together and their product is added to a variable called scalar_product which should be initialized to zero. Your code should stop when it detects end of file in either file that it is reading. For example, if the sequence of integers in one file was "9 7 5 18 13 2 22 16" and "4 7 8 2" in the other file, your code would compute: 4*9 + 7*7 + 8*5 + 2*18 and thus store 161 into scalar_product.

numfile1 = open("numbers1.txt", "r") numfile2 = open("numbers2.txt", "r") scalar_product = 0 number1 = numfile1.readline( ) number2 = numfile2.readline( ) while number1 != " " and number2 != " ": [Tab Key]scalar_product += int(number1)*int(number2) [Tab Key]number1 = numfile1.readline( ) [Tab Key]number2 = numfile2.readline( ) numfile1.close( ) numfile2.close( )

Open the file hostdata.txt for reading.

open('hostdata.txt', 'r')

Open the file winterdata.txt for reading.

open('winterdata.txt', 'r')

Write a statement to open the file yearsummary.txt in a way that erases any existing data in the file.

open('yearsummary.txt', 'w')

Use the file object output to write the string "3.14159" to a file called pi.

output = open('pi', 'w') output.write('3.14159')

Store four file objects corresponding to the files winter2003.txt, spring 2003.txt, and fall2003.txt in the variables winter, spring, summer, and fall (respectively), and open them all for reading.

winter = open('winter2003.txt', 'r') spring = open('spring2003.txt', 'r') summer = open('summer2003.txt', 'r') fall = open('fall2003.txt', 'r')


Ensembles d'études connexes

Unit 6: Florida Real Estate Exam

View Set

OCN screening and early detection

View Set

State Laws, Rules, and Regulations

View Set

PHA 404 Human Physiology Ch 9 Skeletal Muscle MC Only

View Set