Answers for "how to read a text file on python"

29

get text from txt file python

with open ("data.txt", "r") as myfile:
    data = myfile.read().splitlines()
Posted by: Guest on December-10-2020
1

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')
Posted by: Guest on November-27-2020

Code answers related to "how to read a text file on python"

Python Answers by Framework

Browse Popular Code Answers by Language