1

My code pretty much asks user input and a pre made text which it prints to the .txt file. I want to add the character calculator to print into the file, not as a print. Is it possible?

input = input(str("Type something: "))
file = open("harjoitus.txt", "w")
file.write(str("This code is here automatically.\n"))
file.write(str(input))
file.write(str("\n",))
file.close()

with open("harjoitus.txt", 'r') as file:
    text = file.read().strip().split()
    len_chars = sum(len(word) for word in text)
    print(len_chars)

This pretty much prints the amount of characters as print not to the text file how I want it. Is it possible to edit this somehow that it prints the character amount straight to the file and not as a print?

Koppis
  • 109
  • 8
  • 2
    change ```print(len_chars)``` to ```file.write(len_chars)``` – Kurtis Streutker Oct 15 '18 at 19:15
  • 2
    The `print()` command is for printing something to console. If you want to write into a file, you can use `f.write()` as in [the docs](https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects) – G. Anderson Oct 15 '18 at 19:15
  • @KurtisStreutker doesn't work, even if I change it to "w" – Koppis Oct 15 '18 at 19:19
  • @G.Anderson I know that it does, that's why I'm asking how can I make it to print it to the file, as I don't know how to. – Koppis Oct 15 '18 at 19:20
  • Are you trying to write to a new file, or append to the existing file? – G. Anderson Oct 15 '18 at 19:21
  • I want to add it to the existing file – Koppis Oct 15 '18 at 19:23
  • Possible duplicate of [How do you append to a file?](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file) – chickity china chinese chicken Oct 15 '18 at 19:29
  • What is `teksti` – vash_the_stampede Oct 15 '18 at 19:30
  • edited 'teksti' to 'input' - Sorry I have the original version as Finnish text, forgot to translate it to English. I translate the texts to English when asking here so it's easier for you guys to read it. It pretty much just prints the text input that you give – Koppis Oct 15 '18 at 19:34
  • 1
    Note: `input = input(str("Type something: "))` is *asking* for trouble. Don't name-shadow built-ins, especially built-ins you're *actively using*! **Edit:** Ah, in your actual code it's a Finnish name that doesn't shadow anything. – ShadowRanger Oct 15 '18 at 19:34

1 Answers1

1

First before you go into this appending, take a look at how many places you are calling str() its unnecessary most of these values are already stings and ready to be written. Also avoid variable names like input that have preassigned purposes in python. But to add this count to the end, collections.Counter is an option, you should open the file as a append. Then you can add this number to the end of your file.

from collections import Counter

user = input("Type something: ")
with open('harjoitus.txt', 'w') as f:
    f.write("This code is here automatically.\n")
    f.write(user)
    f.write("\n")

with open('harjoitus.txt', 'r') as f:
    content = f.read()

c = Counter(content)
tot = sum(c.values())

with open('harjoitus.txt', 'a') as f:
    f.write(str(tot))
chrx@chrx:~/python/stackoverflow/10.11$ python3.7 stack.py 
Type something: vash
chrx@chrx:~/python/stackoverflow/10.11$ cat harjoitus.txt 
This code is here automatically.
vash
38
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20