
Python File Handling: Reading and Writing Text Data Tutorial
Learn how to read and write text data in files using Python. Understand the built-in functions like open() and close(), and explore methods like read(), readline(), and write() to manipulate file content efficiently. Dive into file handling concepts with this comprehensive guide by Professor John Carelli from Kutztown University.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
File Access Python can read and store information in files It is possible to read and write all kinds of information in files But we will focus mainly on text data Built-in functions: open() and close() Professor John Carelli, Kutztown University
open() open() function is used to create a new file object It takes 2 arguments A string containing the file name and location The access mode for the file (read, write, append) It returns a file object that is then used to access the file Professor John Carelli, Kutztown University
open() Syntax: fobj= open(fileNameString, accessMode) accessMode (3 types, each a simple character): r read only w write Create the file if it doesn t exist If it does exist, all existing information is overwritten and lost a append Create the file if it doesn t exist If it does exist, retain existing information and add new data to the end Professor John Carelli, Kutztown University
Reading from a file with read() read() File object method for reading from a file returns a string containing the entire file contents Example: # create the file object fobj = open( input.txt , r ) # use the object to read the entire file into a string fstr=fobj.read() Professor John Carelli, Kutztown University
Read file using a for loop Read lines of a file one at a time using readline() Example: # create the file object fobj = open( input.txt , r ) # loop to read each line fstr=fobj.readline() # line is stored in string while fstr != : # null string means eof fstr=fobj.readline() # read next line Professor John Carelli, Kutztown University
or use for-in No need to call the read() or readline() methods Example: # create the file object infile = open( input.txt , r ) # loop to read each line for line in infile: # line contains a line of the file print(line) Professor John Carelli, Kutztown University
Using a file object for writing To write to a file, use the write() method It works like print(), except the output goes to the file Unlike print() you must convert items to strings first It returns the number of characters written Example: # create the file object fobj = open( output.txt , w ) # use the object to write something to the file fobj.write( Hello World! ) fobj.write(str(3.14)) Professor John Carelli, Kutztown University
flush() and close() Python doesn t always write the data to the file immediately - It is internally buffered flush() method writes out buffered data immediately Example: fobj.flush() When you are done with the file, you should always close it with the close() method! Forces Python to write out any remaining data (flushes it) Frees up system resources used to manage the object Example: fobj.close() Professor John Carelli, Kutztown University
with statement Use with as an alternative way to open a file Advantage: no need to close the file It closes automatically when the with block is completed Example: with open( input.txt , r ) as infile: for line in infile: # line contains a line of the file print(line) Professor John Carelli, Kutztown University