“python print on file” Ответ

Файл Python открыт

#there are many modes you can open files in. r means read.
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()
Dr. Hippo

python print on file

import sys

print('This message will be displayed on the screen.')

original_stdout = sys.stdout # Save a reference to the original standard output

with open('filename.txt', 'w') as f:
    sys.stdout = f # Change the standard output to the file we created.
    print('This message will be written to a file.')
    sys.stdout = original_stdout # Reset the standard output to its original value
wolf-like_hunter

Python сохранить вывод для файла

with open("output.txt", "a") as f:
    print("Hello StackOverflow!", file=f)
    print("I have a question.", file=f)
Inexpensive Ibis

Ответы похожие на “python print on file”

Вопросы похожие на “python print on file”

Больше похожих ответов на “python print on file” по Python

Смотреть популярные ответы по языку

Смотреть другие языки программирования