Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write function called append_to_file that accepts #two parameters: a filename and some data that will #be an integer or a string to write. The function

Write function called "append_to_file" that accepts
#two parameters: a filename and some data that will
#be an integer or a string to write. The function
#should open the file and add the data to the end of
#the file. Each new call to append_to_file should add
#the new contents on a new line.
#
#Hints:
#
# - Don't forget to close the file when you're done!
# - If the data isn't a string already, you may need
#   to convert it.
# - Remember, this code has no print statements, so
#   when you run it, don't expect to see any output
#   on the right! You could add print statements if
#   you want a confirmation the code is done running.
# - You can put the variable for the filename in the
#   same place where we put text like OutputFile.txt
#   in the videos.


#MY function here!

def append_to_file(filename, data):
   output_file = open(filename, 'a')
   output_file.append(str(data)+'\n')
   output_file.close()


#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print nothing. However, if you open AppendToFileOutput.txt
#in the top left after running it, the contents of the
#file should be another instance of 1301 every time you
#run this file.
append_to_file("AppendToFileOutput.txt", 1301)

 

####

I get the following error...

Traceback (most recent call last):
  File "AppendToFile.py", line 39, in
    append_to_file("AppendToFileOutput.txt", 1301)
  File "AppendToFile.py", line 26, in append_to_file
    output_file.append(str(data)+'\n')
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
Command exited with non-zero status 1

 

I don't know how to fix this. Please help. 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis And Decision Making

Authors: Christian Albright, Wayne Winston, Christopher Zappe

4th Edition

538476125, 978-0538476126

More Books

Students also viewed these Programming questions

Question

A car engine is rated at 110 kW. What is the power in hp?

Answered: 1 week ago