0% found this document useful (0 votes)
56 views

Function Arguments

The document discusses Python functions, arguments, and file operations. It covers default arguments, keyword arguments, arbitrary arguments, opening, reading, writing, and closing files. Key points include setting default values for function arguments using assignment (=), opening files returns a file handle, and the 'with' statement automatically closes files. Reading and writing files requires opening them first and calling methods like read() or write().
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Function Arguments

The document discusses Python functions, arguments, and file operations. It covers default arguments, keyword arguments, arbitrary arguments, opening, reading, writing, and closing files. Key points include setting default values for function arguments using assignment (=), opening files returns a file handle, and the 'with' statement automatically closes files. Reading and writing files requires opening them first and calling methods like read() or write().
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Function

Arguments

File
Operations
Default Arguments

• Function arguments can have default values in Python.


• We can provide a default value to an argument by using the assignment
operator (=).

ADD A FOOTER 2
Example

ADD A FOOTER 3
Python Keyword Arguments

• When we call a function with some values, these values get assigned to
the arguments according to their position.

ADD A FOOTER 4
Python Arbitrary Arguments

• Sometimes, we do not know in advance the number of arguments that will


be passed into a function.
• Python allows us to handle this kind of situation through function calls with
arbitrary number of arguments.
• In the function definition we use an asterisk (*) before the parameter name
to denote this kind of argument.

ADD A FOOTER 5
File Operations

• File is a named location on the system storage which records data for later
access. It enables persistent storage in a non-volatile memory.

ADD A FOOTER 6
File Processing Order

1. Open a file which returns a filehandle.


2. Use the handle to perform read or write action.
3. Close the filehandle.

ADD A FOOTER 7
Open A File In Python

• To read or write to a file, you need to open it first. To open a file in Python,
use its built open() function.
• This function returns a file object, i.e., a handle.
file object = open(file_name [, access_mode][, buffering])

ADD A FOOTER 8
Parameter Details.

• <access_mode>- It’s an integer representing the file open mode, e.g.,


read, write, append, etc.
• It’s an optional parameter.
• By default, it is set to read-only <r>.
• In this mode, we get data in text form after reading from the file. On the
other hand, the binary mode returns bytes.

ADD A FOOTER 9
Parameter Details

• <buffering>- The default value is 0, which means buffering won’t happen. If


the value is 1, then line buffering will take place while accessing the file.

ADD A FOOTER 10
Parameter Details

• <file_name>- It’s a string representing the name of the file you want to
access.

ADD A FOOTER 11
Python File Encoding

• Quote the desired encoding while opening a file in Python.


f = open('app.log', mode = 'r', encoding = 'utf-8')

ADD A FOOTER 12
Close A File In Python

• It’s always the best practice to close a file when your work gets finished.
The Close() File Method

ADD A FOOTER 13
Close With Try…Catch

• Say, if an exception occurs while performing some operations on the file.


In such a case, the code exits without closing the file.
• So it’s better to put the code inside a <try-finally> block.

ADD A FOOTER 14
Auto Close Using ‘With’

• Another way to close a file is by using the WITH clause.


• It ensures that the file gets closed when the block inside the WITH clause
executes.
• The beauty of this method is that it doesn’t require to call the close()
method explicitly.

ADD A FOOTER 15
Perform Write Operation

• While you get ready for writing data to a file, first of all, open it
The Write() File Method
Python provides the write() method to write a string or sequence of bytes to
a file.
This function returns a number, which is the size of data written in a single
Write call.

ADD A FOOTER 16
Example: Read/Write To A File In Python

ADD A FOOTER 17
Perform Read Operation

• To read data from a file, first of all, you need to open it in reading mode.
Then, you can call any of the methods that Python provides for reading
from a file.

ADD A FOOTER 18

You might also like