how to print a text file in python code example

Example 1: python write to file

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

Example 2: open text file in python

f=open("Diabetes.txt",'r')
f.read()

Example 3: print in text file python

price = 33.3
with open("Output.txt", "w") as text_file:
    text_file.write("Purchase Amount: %s price %f" % (TotalAmount, price))

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: Write a Python program to read an entire text file

# Program to read entire file
import os
PATH = "H:\\py_learning\\interviewsprep"
os.chdir(PATH)
def file_read(fname,mode='r+'):
    try:
        with open(fname) as txt:
            print(txt.read())
            print('>>>>>>>>>>>>>>>')
    except FileNotFoundError:
        print("check file existance in current working directory i.e : ",os.getcwd())
        print('provide file existance path to PATH variable')
    finally:
        pass
file_read('file1.txt')

Example 6: python output to text file

$ python my_program.py > output.txt