python txt write code example

Example 1: python writing to text file

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())

Example 2: python make txt file

file = open("text.txt", "w") 
file.write("Your text goes here") 
file.close() 
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists

Example 3: how to write to a text file in 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

Example 4: reading and writing data in a text file with 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:\Text\MyFile2.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']

Example 5: python output to text file

$ python my_program.py > output.txt