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']

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
python reading and writing text file write txt python python writing text file writing data to txt file in python how to write to a txt file python output a text file in python python open .txt file and write into it python write to text file with open python with open write text file python write into text file python how to read text file and write to another text file create a text file in python and write to it write text in a python file saving text file in python create a txt file in python and write to it use text file in python python read text file and write to another text file text file to text file in python how to output a text file in python how to write to a text file using python Write a program that reads the data from the text file.py python write txt file to project write a text file in python with open with open write text file python python write into txt file save text to file, python how to write contents to text file in python write file txt python how to make a text file with python and write it how to print text to file python python read and write text how to write in to txt file in python how to use read and write txt file in python print in text file python write in txt python with open writing text file using python python write text to file wrting text file in python pythion open text file for reading and writing write and read text file python writea text file python python write to to txt save text in file python how to writ in a txt file what we read from another txt file in python how to make a txt file in function python write txt python with open how to load text into file python txt write and read python write to satabase from text file python open txt file and write writing text file in python write to text file using python how to write text to a file in python python write into txt write data into txt file python how to print a text file in python print text file in python python open txt and write make text file in python for writing how to write to txt file in python read and write text python write in a text document python how to create and write in text file in python how to write in text file in python how to create a python script that reads from text file python how to write a text in file with python write to txt python write text to a file in python writing to a text file python how to write text to file in python to text file in python write txt file python python write text to a file write to text file python string ''' python txt write how to read and write text files in in python python to text file write to a text file in python print data to text file python write to an text file python how to write into a text file in python python with open write to text file write data in text file python how to write and read a .txt file how to write a txt file in python python writing to text file write text into file in python python writing to a text file python open txt file and write python open write text file saving a text file in python read and write from text file in python how to read and write text file in python how to write text file in python how to read and write from a text file using python python write .txt how to write on a text file in python how to write on a text file on python how to write on a txt file on python how to write data to a text file in python write a text file python how to write in a text file python txt write python how to store python data to a text file how to write stuff in a text file python python text file write how to write in txt using python how to write in a .txt in python writing to a text file in python python write txt file with open write to a new txt file python write in text file python read a text file and write python output to text file writing to txt python how to open a text file for reading and writing open a text file to read and write save text file in python python write to text file python write to txt how to write to txt file py how to write text in files python using with method? how to write text in files python? python write to text file. how to write a text file in python open and write in text file python how to write values from a function to a text file in python how to write into text file in python how to write in txt files with python how to open file for reading and writing python how to write in text file python write text to a file python what can you write to a txt file in python how to write to a txt file in python writing to text files python how to write and read from text python how to write text files in python python write to txt file writing a text file in python python write to a txt file how to write to txt file python write text data file in python python write data to text file python write text files how to read a file from string and write it to text file python pasting data to text file in python python how to write in a new text file how to write in a txt file python how to read and write to a txt file in python write in txt python write on text file python Open and read the `output.txt` file and write its contents to a new file in python output to text file python write text file in python to text file python how to write text into a file, python write into a text file python how to write a text file in python from a function data writing to text file python python write in file txt write .txt file python write in a txt file python write a text file in python Write a program to read a text file and output the contents in python. write text to file python write to text file python write text file python write data to text file in python how to write to a text file in python python write text file how to write something to a txt file in python python write and read txt files write to a text file python how to read and write from a text file in python write to txt file python python read and write text file how to write in a text file in python open a text file and over write python how to write a text file with python write in a text file python how to write text into a file in python python write text file with open how to save result in text file python reading and writing files in python read text from file python reading and writing file in python oputput to a text file python read text file in python geeksforgeeks read text file python read text file in python reading from terminal in nodejs reading from stdin in node python requests read the docs speech to text and gui in python writing and understanding the xml path of web elements for robot automation testing reading and writing data in a text file with python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source