“Cómo imprimir en un archivo en Python” Código de respuesta

Python escribir en el archivo

file = open(“testfile.txt”,”w”) 
 
file.write(“Hello World”) 
file.write(“This is our new text file”) 
file.write(“and this is another line.”) 
file.write(“Why? Because we can.”) 
 
file.close() 
Misty Macaw

Python imprimir en archivo

import sys

# only this print call will write in the file
print("Hello Python!", file=open('output.txt','a'))

# not this one (std output)
print("Not written")

# any further print will be done in the file
sys.stdout = open('output.txt','wt')
print("Hello Python!")
VasteMonde

Impresión de salida Python a archivo

print("Hello stackoverflow!", file=open("output.txt", "a"))
print("I have a question.", file=open("output.txt", "a"))
Jolly Jellyfish

Cómo imprimir en un archivo en Python

#you do not have to create a text file in advance, this program will create the file if it does not exist.
whatever = ('this is what will be printed to your file')
sourceFile = open('demo.txt', 'w')
print(whatever, file = sourceFile)
sourceFile.close()
i dont know what to put here

Python imprimir en archivo

# Print to the text file
file = open('log.txt', 'w')
print('This is a sample print statement', file = file)
print('Hello World', file = file)

file.close()
Gorgeous Gazelle

Respuestas similares a “Cómo imprimir en un archivo en Python”

Preguntas similares a “Cómo imprimir en un archivo en Python”

Más respuestas relacionadas con “Cómo imprimir en un archivo en Python” en Python

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código