[go: up one dir, main page]

0% found this document useful (0 votes)
102 views15 pages

Python File Handling Basics

The document discusses file handling in Python. It defines what a file is and the two main types - text and binary. It explains that files persist on the hard disk even after powering off. The key steps for handling files are opening, reading/writing data, and closing. It describes how to open files in different modes, read data using methods like read(), readline(), readlines(), and write data using write() and writelines(). Loops can iterate through file data. Checking if a file exists and closing files properly releases system resources.

Uploaded by

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

Python File Handling Basics

The document discusses file handling in Python. It defines what a file is and the two main types - text and binary. It explains that files persist on the hard disk even after powering off. The key steps for handling files are opening, reading/writing data, and closing. It describes how to open files in different modes, read data using methods like read(), readline(), readlines(), and write data using write() and writelines(). Loops can iterate through file data. Checking if a file exists and closing files properly releases system resources.

Uploaded by

Syeda Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

File Handling

Dr. Arham Muslim

Innovative Technologies in Learning, SEECS, NUST


http://itl.seecs.nust.edu.pk/
Introduction

RAM
Hard Disk (SSD/HDD)
Var Var List

Program
Secondary Storage

Data only available until Data remain even after


program is running power is turned off!!

File Handling 2
File Handling
• File is a computer resource for recording data in a computer storage device
(e.g., hard disk)
• Any kind of information can be written to a file
• A file stored in a hard disk persist even after power is turned off

• Two broad types of files


• Text files contains data viewable in a text editor and understandable by humans

• Binary files contains a collection of binary information that is computer-readable

File Handling 3
File Handling Steps
• Handling file in Python consists of following three steps

Reading/Writing
Open a file Close the file
data from/to file

File Handling 4
Opening a File
• The key function for working with files in Python is the open() function
• General syntax

• File name is the name of the file or path to the file if it is not in the same directory
• Mode specifies the type operation you want to perform on the file
• A file can only be opened in a single mode at a time

• Modes of opening file


Mode Title Description
"r" Read Opens a file for reading, error if the file does not exist (Default value)
"a" Append Opens a file for appending, creates the file if it does not exist
"w" Write Opens a file for writing, creates the file if it does not exist
"x" Create Creates the specified file, returns an error if the file exists

File Handling 5
Opening a File – Example
• Opening file for reading from the same directory

• Opening file for reading from the user folder inside the data folder

• Opening file for writing from the same directory

File Handling 6
Closing a File
• After performing the required operations on the file, it should always be
closed using the close() method
• E.g.

• Closing a file releases valuable system resources

File Handling 7
Reading from a File
• To read from a file, it should be opened using the "r" mode
• The argument "r" can be omitted because it is the default value

• Make sure the file you want to read already exists otherwise a runtime error
will be thrown

• Python provides isfile() function from the os.path module to check if a file
exists or not

File Handling 8
Reading from a File – read()
• Reads the content of the file and return it as a string

• You can get a specific number of character by passing that number as an argument to the
read() function

• Data that has been read cannot be read again. You need to open the file again

File Handling 9
Reading from a File – readline()
• Reads a single line from a file and returns it as a string

• You can get a specific number of bytes (characters) of a line by passing that
number as an argument to the readline() function

File Handling 10
Reading from a File – readlines()
• Reads all the lines of a file and returns them as a list of strings

• A single argument is available to limit the number of lines to return


• If the total number of bytes (characters) returned exceeds the specified
number, no more lines are returned

File Handling 11
For Loop with File
• For loop can be used to iterate through the file data

File Handling 12
Writing to a File
• To write to a file, it should be opened using the "a", "w", or "x" mode
Mode Title Description
"a" Append • Opens a file for appending
• Creates the file if it does not exist
• New data is added at the end of old data
"w" Write • Opens a file for writing
• Creates the file if it does not exist
• Old data is deleted when new data is added
"x" Create • Creates the specified file
• Returns an error if the file exists

• Python provides two methods to write to a file


• write(string)
• writelines(lines)

File Handling 13
Write to a File – write()
• The write() method writes any string to an open file

File Handling 14
Write to a File – writelines()
• writelines() allow you to write multiple lines to a file
• It writes all the items of a list to the file

• Moving text to new line should be handled in the data. Python will not
automatically move each item to new line

• The list should only contain string items, any other type of item will throw
an error

File Handling 15

You might also like