python append to file
with open(filename, "a+") as f:
f.write('Hello World')
python append to file
with open(filename, "a+") as f:
f.write('Hello World')
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')
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: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']
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us