Hello I'm working on files and extension in my python code along with text files. In my text file I have to make "data1.txt". What would I need to write in my python code to create both possible outputs? Because when I add count, it includes line numbers but I only need to see that the second time I open the code, when I reopen it. And at the end where it says there are 8 lines, do I include that in the code or text file? I also have to include def main().

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Hello I'm working on files and extension in my python code along with text files. In my text file I have to make "data1.txt". What would I need to write in my python code to create both possible outputs? Because when I add count, it includes line numbers but I only need to see that the second time I open the code, when I reopen it. And at the end where it says there are 8 lines, do I include that in the code or text file?

I also have to include def main().

Here is the text from data1.txt:

This is a data file

When you process your data file remember to strip the end of the line character.
rstrip method is a string method that strips a character from end of the string

line1 = line1.rstrip('\n')

To handle any potential error use try/except/else

def file_name_function(file_name):
if file_name == 'data1.txt':
inFile =open('data1.txt', 'r')
file_contents = inFile.readline()
count = 1
while file_contents != '';
main ()
print("line", count, file_contents)
file_contents = inFile.readline()
count+=1
inFile.close()
elif file_name == 'data2.txt':
inFile = open('data2.txt', 'r')
file_contents = inFile.readline()
while file_contents != '':
print (file_contents)
file_contents = inFile.readline()
inFile.close()
def main ():
file_name = str(input("Enter the name of the file: "))
file_name_function(file_name)
Transcribed Image Text:def file_name_function(file_name): if file_name == 'data1.txt': inFile =open('data1.txt', 'r') file_contents = inFile.readline() count = 1 while file_contents != ''; main () print("line", count, file_contents) file_contents = inFile.readline() count+=1 inFile.close() elif file_name == 'data2.txt': inFile = open('data2.txt', 'r') file_contents = inFile.readline() while file_contents != '': print (file_contents) file_contents = inFile.readline() inFile.close() def main (): file_name = str(input("Enter the name of the file: ")) file_name_function(file_name)
Enter the name of the file: datal.txt
The contents of the data file datal.txt are listed below:
This is a data file
When you process your data file remember to strip the end of the line character.
rstrip method is a string method that strips a character from end of the string
linel = linel.rstrip ('\n')
To handle any potential error use try/except/else
closing datal.txt
Reopening datal.txt
linel This is a data file
line2
line3 When you process your data file remember to strip the end of the line character.
line4 rstrip method is a string method that strips a character from end of the string
line5
line6 linel = linel.rstrip ('\n')
line7
line8 To handle any potential error use try/except/else
There are 8 lines in file datal.txt
Transcribed Image Text:Enter the name of the file: datal.txt The contents of the data file datal.txt are listed below: This is a data file When you process your data file remember to strip the end of the line character. rstrip method is a string method that strips a character from end of the string linel = linel.rstrip ('\n') To handle any potential error use try/except/else closing datal.txt Reopening datal.txt linel This is a data file line2 line3 When you process your data file remember to strip the end of the line character. line4 rstrip method is a string method that strips a character from end of the string line5 line6 linel = linel.rstrip ('\n') line7 line8 To handle any potential error use try/except/else There are 8 lines in file datal.txt
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY