Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing integer to file
#1
I open a file for appending:
file = open(filename, "a")
I have a numeric
CurrentCount=300
This prints 300
print (CurrentCount)
This outputs a lot of digits, obviously not decoded into ASCII
file.write(str(CurrentCount))
What does? I want readable number in ASCII.

I'm using Python 3.7
Reply
#2
You open the file as mode 'a' which means append.
It will create the file if it doesn't exist, but if it does exist, it will retain contents and append new contents.
you should open as mode 'w' if you don't want this.

def write_file(filename, intval):
    with open(filename, 'w') as fp:
        print(f'intval: {intval}')
        fp.write(str(intval))

def read_file(filename):
    with open(filename) as fp:
        return fp.read()

if __name__ == '__main__':
    myfile = 'tryit.txt'
    CurrentCount = 300
    write_file(myfile, CurrentCount)
    print(f'reading result: {read_file(myfile)}')
output:
Output:
intval: 300 reading result: 300
hexdump:
Output:
$ hd tryit.txt 00000000 33 30 30 00000003
Reply
#3
Yes! So stupid of me. In the actual code, the number was constantly changing so I didn't realize that what I was seeing was just multiple tries continuing on the same line. It hit me when I went to bed so I couldn't sleep and just after midnight I got up and just had to try a write instead of an append since all I wanted was a single line in the file. I was hoping I would solve my stupidity before someone else discovered it but you beat me to it. Thanks.
Reply
#4
Quote:It hit me when I went to bed so I couldn't sleep and just after midnight I got up and just had to try a write instead of an append since all I wanted was a single line in the file
Sign of a true programmer!
I've been at it since 1968, and I can't count the number of times I did this.
Reply
#5
(Apr-02-2019, 07:21 AM)sritaa Wrote: Read Only ('r') : Open text file for reading. ...
Read and Write ('r+') : Open the file for reading and writing. ...
Write Only ('w') : Open the file for writing. ...
Write and Read ('w+') : Open the file for reading and writing. ...
Append Only ('a') : Open the file for writing
Append and Read (‘a+’) : Open the file for reading and writing.
REDACTED
Thanks. I know that - been using unix for 24 years. I originally meant to append the info to the file (but forgot that there was no newline in it so the append just added numbers to the same line. However, I figured out I dodn't need more than one piece of information and forgot to change the append to a write.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,364 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 995 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,481 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,683 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,737 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,407 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,775 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,364 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,513 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  Help! Formatting and Writing to a File bwdu 2 2,408 Apr-19-2020, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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