Saltar al contenido

escribir en el archivo txt ejemplo de código python

Luego de mucho luchar hemos dado con el resultado de esta cuestión que agunos lectores de este espacio han presentado. Si tienes algún detalle que compartir no dejes de aportar tu conocimiento.

Ejemplo 1: Python escribiendo en un archivo de texto

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())

Ejemplo 2: Python escribe en un 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()

Ejemplo 3: cómo escribir en un archivo de texto en python

#the way i learned it#plus explanations

from sys import * #this is for making it more flexible#so now we gotta open a file to write in

f = open("haha.txt", "w")# the w means WRITING#now we gotta write something

f.write(str(argv[1]))# the str means change it into a string of letters#argv[1] means it's the thing you type after the python run thing#for example: "python run.py helo"#now we gotta finish it
f.close()#if you want to see whats in it...

f = open("haha.txt", "r")#r means READING
print(f.read())# this prints what you wrote#example input and output#            python run.py helo#   helo

Ejemplo 4: leer y escribir datos en un archivo de texto con Python

#for reading and writing data in a text file with python#First you must have a file Open or create a new file have it loaded in memory.# Open function to open the file "MyFile1.txt" # (same directory) in append mode and 
file1 = open("MyFile.txt","a")# store its reference in the variable file1 # and "MyFile2.txt" in D:Text in file2 
file2 = open(r"D:TextMyFile2.txt","w+")# Opening and Closing a file "MyFile.txt" # for object name file1. 
file1 = open("MyFile.txt","a") 
file1.close()# Program to show various ways to read and # write data in a file. 
file1 = open("myfile.txt","w") 
L =["This is Delhi n","This is Paris n","This is London n"]# n is placed to indicate EOL (End of Line) 
file1.write("Hello n") 
file1.writelines(L) 
file1.close()#to change file access modes 

file1 = open("myfile.txt","r+") 

print "Output of Read function is "
print file1.read() 
print

# seek(n) takes the file handle to the nth # bite from the beginning. 
file1.seek(0) 

print "Output of Readline function is "
print file1.readline() 
print

file1.seek(0)# To show difference between read and readline 
print "Output of Read(9) function is "
print file1.read(9) 
print

file1.seek(0) 

print "Output of Readline(9) function is "
print file1.readline(9) 

file1.seek(0)# readlines function 
print "Output of Readlines function is "
print file1.readlines() 
print
file1.close()# Python program to illustrate # Append vs write mode 
file1 = open("myfile.txt","w") 
L =["This is Delhi n","This is Paris n","This is London n"] 
file1.close()# Append-adds at last 
file1 = open("myfile.txt","a")#append mode 
file1.write("Today n") 
file1.close() 

file1 = open("myfile.txt","r") 
print "Output of Readlines after appending"
print file1.readlines() 
print
file1.close()# Write-Overwrites 
file1 = open("myfile.txt","w")#write mode 
file1.write("Tomorrow n") 
file1.close() 

file1 = open("myfile.txt","r") 
print "Output of Readlines after writing"
print file1.readlines() 
print
file1.close() 

Output of Readlines after appending
['This is Delhi n', 'This is Paris n', 'This is London n', 'Today n']

Output of Readlines after writing
['Tomorrow n']

Ejemplo 5: leer y escribir en el archivo python

file= open(“testfile.txt”, “r+”)

Ejemplo 6: salida de Python a un archivo de texto

$ python my_program.py > output.txt

Si crees que te ha resultado provechoso este post, sería de mucha ayuda si lo compartieras con otros juniors así nos ayudas a dar difusión a nuestra información.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *