Appending To Files Challenge - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Appending To Files Challenge (/thread-17057.html) |
Appending To Files Challenge - erfanakbari1 - Mar-26-2019 # I Write a program to append the times table for our poem in sample.txt .So , this is all of my code numbers = 1 for i in range(2, 12): while 13 >= numbers > 0: multiply = numbers * i print('| {0} Times {1} is {2} '.format(numbers, i, multiply)) numbers += 1 print('=' * 21) with open('times_table.txt', 'w') as times: for table in times: print(table, file=times)And the output is : But because of code at the end lines for files appending , I'm facing this error below : So , finally I don't know how to append this time table stuffs into a sample.txt file .I really appreciate you all guys . If you can possibly help me with this . Thanks . RE: Appending To Files Challenge - ichabod801 - Mar-26-2019 You want to have the file open while you are generating the lines of the multiplication table, and you want to write them to the file (times.write()) instead of printing them. If you want to append to a file instead of writing over it, you use mode 'a' when you open the file instead of mode 'w'. RE: Appending To Files Challenge - aankrose - Mar-26-2019 Try using W+ Use W+ , here is the result numbers = 1 for i in range(2, 12): while 13 >= numbers > 0: multiply = numbers * i print('| {0} Times {1} is {2} '.format(numbers, i, multiply)) numbers += 1 print('=' * 21) with open('times_table.txt', 'w+') as times: for table in times: print(table, file=times)
RE: Appending To Files Challenge - perfringo - Mar-27-2019 If you need to work with files then it might be useful to get aqcuainted with documentation about open() (which is quite extensive). |