“Python -Druck in der Datei” Code-Antworten

Python -Datei offen

#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 -Druck in der Datei

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 speichern die Ausgabe in Datei

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

Ähnliche Antworten wie “Python -Druck in der Datei”

Fragen ähnlich wie “Python -Druck in der Datei”

Weitere verwandte Antworten zu “Python -Druck in der Datei” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen