subject

For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on your computers you can read and write files. Think of zybooks like a computer without any files in it. If you want to test your code that reads a file you first have to write a file to test. The following describes what each function should do: writeToFile(string, filename) This functions takes the name of a file, opens that file, and the writes the string to that file. This function does not need a loop. This function should not return anything. This function will help you see if your other functions are working correctly. Example writeToFile("Hello, World\n", "hello. txt" first file Hello, World\n printFile(filename) This functions takes the name of a file, opens that file, and then prints the text of the file. This function does not need a loop. This function should not return anything. This function will help you see if your other functions are working correctly. Example: printFile("numbers. txt") numbers. txt output 2 writeListToFile(a_list, filename) This function takes the name of a file and opens that file. Then this function should print each of the numbers in the list a_list on a new ine. Example: writelistToFile([1,2,3,4], "myfile. txt" myfile 2
writeUserInfo (name, password, filename) This function takes 3 parameters, a name, a password, and a file name. This function should open the file then append two lines. The first line should be the text 'name-' followed by the name parameter. The second line should be the text password' followed by the password parameter. Example: writeUserInfo( "paul", "walrus7", "secret_file") secret_file name pau password-walrus7 Example: writeUserInfo( "dave", "python is fun", "secret file") secret file name pau password-walrus7 name-dave password-python is fun sumFile(filename) This function takes a file and opens it. This function should loop through every line in the file. Then cast the line to a number and add it to the sum Example: sumFile( "myfile. txt") should return 10 nameInFile (user, filename) This function takes a user name and a filename. This function should open the file. This should loop over all of the lines. If the line equals name-'user then this function returns True. Otherwise if the name is not in the file it returns False Example nameInFile 'paul, 'secret file') should return True
def writeToFile(string, filename): with open (filename, "w") as x: 2 x. write(string) 4 6 def printFile(filename): f open (filename, "r" for line in f: 7 print(line) 10 f. close() 12 13 14 15 16 17 18 19 20 21 def writelistToFile(l, filename): xopen(filename, 'w') x. writelines ( Ñ—) x. close() def writeUserInfo(name, password, filename): xopen(filename, "w) x. write( name-'+name) x. write( 'passwordpassword) x. close() 23 24 25 26 27 28 29 def sumFile(filename): xopen(filename, "r") num = sum(int return num 31 32 odef nameInFile (user, filename): xopen(filename, "r") if user in x. readlines (): x. close() return True 35 else: 37 38 39 40 41 42 43 x. close) return False def main): 45 46 47 48 49 50 51 52 writeToFile("Hello, World\n", "hello. txt") printFile("hello. txt") writeListToFile([1, 2, 3, 4], "myfile. txt" printFile("myfile. txt") writeUserInfo("paul", "walrus7", "secret_file") writeUserInfo("dave", "python_is_fun", "secret file") printFile("secret file") print(sumFile("myfile. txt")) print (nameInFile( 'paul', 'secret_file'))

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 08:30
1.the index finger on your right hand types the f r v 4 j u m 7 h y 6 n lo.9 j u 7 m g t 5 b 2.if you need to multiply 400, 2, and 1 ½, what would you type on the numeric keypad? 400*2*1.5 400/2*1.5 400/2/1.5 400*2*1½ 3.select all examples of proper keyboarding technique. rest your fingers gently on the home row or home keys. slouch in your chair. rest your palms on the keyboard. relax your fingers. keep your hands lower than your elbows.
Answers: 1
question
Computers and Technology, 22.06.2019 17:30
Type the correct answer in the box. spell all words correctly. under which key category do the page up and page down keys fall? page up and page down keys fall under the keys category.
Answers: 3
question
Computers and Technology, 23.06.2019 01:00
Let r be a robotic arm with a fixed base and seven links. the last joint of r is a prismatic joint, the other ones are revolute joints. give a set of parameters that determines a placement of r. what is the dimension of the configuration space resulting from your choice of parameters?
Answers: 3
question
Computers and Technology, 23.06.2019 12:30
How is the brightness of oled of the diaplay is controled
Answers: 1
You know the right answer?
For today's lab you going to write six functions. Each function will perform reading and/or write to...
Questions
question
Mathematics, 22.01.2021 19:10
Questions on the website: 13722367