Question
(3) Write Data to a new file. - Create a new python file (Q3_file_write), o Create a file variable (fp) and open a file (test1.txt)
(3) Write Data to a new file. - Create a new python file (Q3_file_write), o Create a file variable (fp) and open a file (test1.txt) for w o Write the following three lines into the file line one line two line three o Close the file - Check to see if the file (test1.txt) is created ithe n appropriate locate - Use notepad++ or sublime to open the file and check the content. - What if you want to see the three lines separately in different lines? Modify the code to address than - (4) (Q4_file_read). Read data from the file created above in Q(1), using read(), readline( ) , and readlines() Create a new py file (Q2_file_read) to read the file you created in the above (Note, be sure to use fp.seek(0) to move the pointer to the start before reading). You will learn how to use .strip() to remove the control characters ( , .. ) for the text o Create a file variable (fp) and open a file (test1.txt) for r o Use fp.read() to read (the entire file) and display the entire it o Use fp.readline() to read and print line-by-line o Use comprehensive to create a list[ ] that stripped the Newline ( ) # there is no .read() statement, just a for loop. o Use fp.readlines( ) to read line-by-line and print Results: using .read()--------------- line one line two line three using .readline() ------------ line one line two
line three comprehensive way ----------- ['line one', 'line two', 'line three'] using .readlines() --------------- ['line one ', 'line two ', 'line three '] (5) (Q5_num_data_w) Write a program to read three numbers from the console, convert them to integers, perform some simple math, and save them to a file as strings. - Create a file handler for write mode (w) o Assign filename variable (fn) o Use fn as filename parameter for open (fn, w) o Get three numbers (num1, num2, num3) from the user and cast it into integer o make num1 = num 2 + num3 num2 = num 3 + num1 num3 = num 1 + num2 o Save the results to the file fn You need to convert the numbers to strings before saving the files Open the file (fn) and check the results
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started