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().
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)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3eb03a80-7cae-4d33-b228-e2e0361c370e%2Fc040f056-5b11-4743-bef4-b7c02eb00131%2Fuwdmb5_processed.png&w=3840&q=75)
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F3eb03a80-7cae-4d33-b228-e2e0361c370e%2Fc040f056-5b11-4743-bef4-b7c02eb00131%2Fliot7mu_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
The total number of lines is not a part of the data1.txt file.
The else part in your code is not required as you're trying to handle a case where the input is something other than data1.txt which is unnecessary.
The correct code has been attached below.
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)