Python Forum
Appending To Files Challenge
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Appending To Files Challenge
#1
# 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 :

Output:
| 1 Times 2 is 2 | 2 Times 2 is 4 | 3 Times 2 is 6 | 4 Times 2 is 8 | 5 Times 2 is 10 | 6 Times 2 is 12 | 7 Times 2 is 14 | 8 Times 2 is 16 | 9 Times 2 is 18 | 10 Times 2 is 20 | 11 Times 2 is 22 | 12 Times 2 is 24 | 13 Times 2 is 26 ========================================
But because of code at the end lines for files appending , I'm facing this error below :
Error:
Traceback (most recent call last): File "C:/Users/debug system/IdeaProjects/LearnPY/02/I_O - Challenge.py", line 11, in <module> for table in times: io.UnsupportedOperation: not readable
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 .
Reply
#2
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'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Output:
home/aankrose/code/phase2/venv/bin/python /home/aankrose/code/phase2.py | 1 Times 2 is 2 | 2 Times 2 is 4 | 3 Times 2 is 6 | 4 Times 2 is 8 | 5 Times 2 is 10 | 6 Times 2 is 12 | 7 Times 2 is 14 | 8 Times 2 is 16 | 9 Times 2 is 18 | 10 Times 2 is 20 | 11 Times 2 is 22 | 12 Times 2 is 24 | 13 Times 2 is 26 ===================== Process finished with exit code 0
Reply
#4
If you need to work with files then it might be useful to get aqcuainted with documentation about open() (which is quite extensive).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Team Chooser Challenge kanchiongspider 3 2,312 Jun-02-2020, 04:02 AM
Last Post: kanchiongspider
  Meal cost challenge Emekadavid 3 2,802 Jun-01-2020, 02:01 PM
Last Post: Emekadavid
  Problem with a basic if challenge erfanakbari1 2 1,936 Oct-12-2018, 08:04 AM
Last Post: erfanakbari1
  trying an online challenge tozqo 8 5,873 Jun-21-2017, 07:07 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020