python read and write code example

Example 1: get text from txt file python

with open ("data.txt", "r") as myfile:
    data = myfile.read().splitlines()

Example 2: python read file

# Basic syntax:
with open('/path/to/filename.extension', 'open_mode') as filename:
  file_data = filename.readlines()	# Or filename.read() 
# Where:
#	- open imports the file as a file object which then needs to be read
#		with one of the read options
#	- readlines() imports each line of the file as an element in a list
#	- read() imports the file contents as one long new-line-separated 
#		string
#	- open_mode can be one of:
#		- "r" = Read which opens a file for reading (error if the file 
#			doesn't exist)
#		- "a" = Append which opens a file for appending (creates the 
#			file if it doesn't exist)
#		- "w" = Write which opens a file for writing (creates the file 
#			if it doesn't exist)
#		- "x" = Create which creates the specified file (returns an error
#			if the file exists)
# Note, "with open() as" is recommended because the file is closed 
#	automatically so you don't have to remember to use file.close()

# Basic syntax for a delimited file with multiple fields:
import csv
with open('/path/to/filename.extension', 'open_mode') as filename:
	file_data = csv.reader(filename, delimiter='delimiter')
    data_as_list = list(file_data)
# Where:
#	- csv.reader can be used for files that use any delimiter, not just
#		commas, e.g.: '\t', '|', ';', etc. (It's a bit of a misnomer)
#	- csv.reader() returns a csv.reader object which can be iterated 
#		over, directly converted to a list, and etc. 

# Importing data using Numpy:
import numpy as np
data = np.loadtxt('/path/to/filename.extension',
				delimiter=',', 	# String used to separate values
				skiprows=2, 	# Number of rows to skip
				usecols=[0,2], 	# Specify which columns to read
				dtype=str) 		# The type of the resulting array

# Importing data using Pandas:
import pandas as pd
data = pd.read_csv('/path/to/filename.extension',
				nrows=5, 		# Number of rows of file to read
				header=None, 	# Row number to use as column names 
	            sep='\t', 		# Delimiter to use 
	            comment='#', 	# Character to split comments
				na_values=[""])	# String to recognize as NA/NaN

# Note, pandas can also import excel files with pd.read_excel()

Example 3: python file reading

fin = open("NAME.txt", 'r')
body = fin.read().split("\n")
line = fin.readline().strip()

Example 4: python file open

#there are many modes you can open files in. r means read.
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()

Example 5: read and write to file python

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

Example 6: read and write file in python

#Write a Python program to create a file of numbers by taking input from the user and then display the content of the file. You can take input of non-zero numbers, with an appropriate prompt, from the user until the user enters a zero to create the file assuming that the numbers are non-zero.  
    f = open ('NumFile.txt','w')
    while True :
        no = int(input("enter a number (0 for exit)"))
        if no == 0 :
            print("you entered zero(0) ....... \n now you are exit  !!!!!!!!!!!")
            break
        else :
            f.write(str(no)+"\n")
    f.close() 
    f1 = open ('NumFile.txt','r')
    print("\ncontent of file :: \n",f1.read())
    f1.close()