#๐Ÿ”’ Question with reading

25 messages ยท Page 1 of 1 (latest)

raw sandal
#

why is ap and apple next to each other without empty space between them(in output)

def FirstExample():
object = input("What kind of object? ")
with open("5.1.txt","a") as f:
f.write(f"{object}\n") # \n so that it writes every new line

with open("5.1.txt", "r") as f:
    print(f.read()) # You can read a file only in "r" mode, you can't do that in "a" for example
    f.seek(0)
    print(f.read(10))
    f.seek(0)
    print(f.readline(2))# Reads up to 2 characters from the current line
    f.seek(0)
    print(f.readline())
    f.seek(0)
    for _ in range(3):
        print(f.readline(), end="")

FirstExample()

stuck tundraBOT
#

@raw sandal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hazy bison
#

what "ap" and "apple"?

raw sandal
#

in output when you run code

hazy bison
#

well that would depend on the input, right?

#

I have no way of knowing what's in that file

raw sandal
#

oh yeah mb let me put in image

hazy bison
#

ok so what in that output is unexpected?

#

and what is the actual content of the file like?

raw sandal
#

the content is i just write one word everytime i run it so its all of the above up to xyn

hazy bison
#

but the code reads from the beginning several times

#

so seeing what's actually in the file might help

raw sandal
hazy bison
#

ok, and now tell me what in the output you disagree with

raw sandal
#

why is there no empty space between ap and apple

#

like between ap,apple and next 3 words

hazy bison
#

which part of the code outputs that?

raw sandal
#

this i think

#

print(f.readline(2))# Reads up to 2 characters from the current line
f.seek(0)
print(f.readline())

hazy bison
#

you think? why not just comment out everything else and be sure?

raw sandal
#

what to cmment out

hazy bison
#

well all the other sections that read from the file and print stuff?

stuck tundraBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.