0% found this document useful (0 votes)
26 views29 pages

Python Text File Operations Guide

This document discusses file handling in Python, including opening, reading from, and writing to text files. It covers opening files for reading and writing, using methods like read(), readline(), writelines() to process file contents. Examples are provided to demonstrate reading file lines and writing multiple lines to a file in Python. The key aspects of working with text files in Python include opening, reading/writing operations, and closing files.

Uploaded by

Jayashri K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views29 pages

Python Text File Operations Guide

This document discusses file handling in Python, including opening, reading from, and writing to text files. It covers opening files for reading and writing, using methods like read(), readline(), writelines() to process file contents. Examples are provided to demonstrate reading file lines and writing multiple lines to a file in Python. The key aspects of working with text files in Python include opening, reading/writing operations, and closing files.

Uploaded by

Jayashri K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Course Code

Problem Solving Using Python

Course Code – Course Name Prepared by:


Topics
• Text Files
– File open
– File writing
– File Close

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text Files
• A text file is a file containing characters,
structured as lines of text.
• It contains printable characters, nonprinting
newline character, \n, to denote the end of
each text line.
• Text files can be directly viewed and created
using a text editor

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Binary Files
• Binary files can contain various types of data,
such as numerical values, and are therefore
not structured as lines of text.
• These files can only be read and written via a
computer program.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
File Operations
• Fundamental operations of all types of files
include
– opening a file,
– reading from a file,
– writing to a file, and
– closing a file.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Opening Text Files
• All files must first be opened before they can be
read from or written to.
• In Python, when a file is opened, a file object is
created that provides methods for accessing the
file.

open( ) Used to create a new file for either writing newly or


append the content.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Opening for Reading
Example
• The first argument is the file name to be opened, '[Link]'.

• The second argument, 'r', indicates that the file is to be opened


for reading. (The second argument is optional when opening a
file for reading.)

• If the file name does not exist, then the program will terminate
with a “no such file or directory” error.

• When a file is opened, it is first searched for in the same


folder/directory that the program resides in.
Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Absolute paths can also be provided by giving the location of a
file anywhere in the file system.

Example

When the program has finished reading the file, it should be


closed by calling the close method on the file object.

Once closed, the file may be reopened by the same, or another


program.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Opening for Writing
• To open a file for writing, the open function is used as shown
below.

• 'w' is used to indicate that the file is to be opened for writing.


• If the file already exists, it will be overwritten.
• When using a second argument of 'a', the output will be
appended to an existing file instead.
• It is important to close a file that is written to, otherwise the
tail end of the file may not be written to the file.

• When opening files for writing, there is not much chance of an


I/O error occurring. The provided file name does not need to
exist since it is being created.
Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Reading Text Files
• The readline method returns as a string the
next line of a text file, including the end-of-line
character, \n.

• When the end-of-file is reached, it returns an


empty string.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
NOTE: The blank lines in the screen output. Since read_line
returns the newline character, and print adds a newline character,
two newline characters are output for each line displayed, resulting in
a skipped line after each.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Writing Text Files
• The write method is used to write strings to a file.

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
• This code copies the contents of the input file, '[Link]', line
by line to the output file, 'myfile_copy.txt'.
• In contrast to print when writing to the screen, the write
method does not add a newline character to the output string .
• Thus, a newline character will be output only if it is part of the
string being written.
• In this case, each line read contains a newline character. Finally,
when writing to a file, data is first placed in an area of memory
called a buffer .
• Only when the buffer becomes full is the data actually written to
the file.
• Since the last lines written may not completely fill the buffer, the
last buffer’s worth of data may not be written.
• The close() method flushes the buffer to force the buffer to be
written to the fi le.
Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Example

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text file writing
• File operations
[Link]
2. write
[Link]
1. write()  used to write one line data into the file
[Link](content)
2. writelines()used to write multiple lines to the file
[Link](multipleline)

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Writing
Example
print ("File Handling")
print ("--------------")
fp=open(“[Link]","w")
[Link]("Good day to all\n")
[Link]("Have a nice time with Python!!!!!!\n")
[Link]("Thank you\n")
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Writing

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Writing
Example
print ("File Handling")
print ("--------------")
fp=open("[Link]","w")
data=["This is from [Link]\n","Python Tutor\n","Python is
somewhat interesting\n","Thank u\n"]
[Link](data)
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Writing

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
• Handling the data files
• File operations
[Link]
2. read
[Link]
 1. open() used to open the file for reading
Variable=open(“filename”,”mode”)
r read mode  read the data from file
[Link]() read the specified number of chars from the file
Variable=[Link]([number])
 [Link]() read the data line by line (one line at a time
Variable=[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
[Link]() read all the lines from the file
Variable=[Link]()
[Link]() used to close the file
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
Example
print ("File Handling")
print ("--------------")
fp=open("[Link]","r")
ans=[Link]()
print (ans)

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
Example
print ("File Handling")
print ("--------------")
fp=open("[Link]","r")
x1=[Link](7)
print (x1)
x2=[Link](10)
print (x2)
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
Example
print ("File Handling")
print ("--------------")
fp=open("[Link]","r")
x1=[Link]()
print (x1)
x2=[Link]()
print (x2)
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading
Example
print ("File Handling")
print ("--------------")
fp=open("[Link]","r")
x=[Link]()
print(x)
[Link]()

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna
Text File Reading

Course- Problem
Code – Course
SolvingName
Using Python Prepared by: Dr. S. Prasanna

You might also like