Похожие чаты

Hi guys i have very strange problem with .read() here is my

code:

myfile = open('D:/myfile.txt', 'r+')

myfile.write('my name is yousef')
print (myfile.read())
myfile.close()

if i run this code the result is nothing. then i have to assign a parameter for read() for example read(n)
and excute the code and then clear that parameter and then this codes returns that line. but this code repeats that string b times in the myfile.txt. for example if i write myfile.read(3) that string comes triple times in the file

what is wrong. where is my problem?

3 ответов

7 просмотров

Sorry friend. I'm not allowed to answer your question.

[anti-anti-spam trigger text] Imagine file operations like you have a carrige. You can read and write with the same carrige. After you wrote a few characters, your carrige will be at the end of the file, so when you call read() you won't read anything. If you want to read back what you just wrote, you have to position the carrige back at the beginning of the text to be able to read it. you can do this by calling myfile.seek(0,0) before reading. [anti-anti-spam trigger text]

Yousef- Автор вопроса

Похожие вопросы

Обсуждают сегодня

Карта сайта