Python Session - 5

Python Session - 5
Anirudha Anil Gaikwad
gaikwad.anirudha@gmail.com
What you learn
 Python Modules
 Python Package
 Python File I/O
Python Modules
Modules refer to a file containing Python statements and definitions.
A file containing Python code, for e.g.: filename.py, is called a module and its module name
would be filename.
We use modules to break down large programs into small manageable and organized files.
Furthermore, modules provide reusability of code.
We can define our most used functions in a module and import it, instead of copying their
definitions into different programs.
Python Modules
 How to import modules in Python?
use the import keyword to import modules in Python
>>> import filename
# import statement example
# to import standard module math
import math
print("The value of pi is", math.pi)
Python Modules
 Import with renaming
We can import a module by renaming it as follows.
import calendar as cal
print("------Calendar Program in Python------n");
print("Enter 'x' for exit.");
y = input("Enter Year: ");
if y == 'x':
exit();
else:
m = input("Enter month: ");
yy = int(y);
mm = int(m);
print("n",cal.month(yy,mm))
Python Modules
 Python from...import statement
from used to import specific names from a module without importing the module as a
whole.
# import only pi from math module
from math import pi
print("The value of pi is", pi)
 Import all names
# import all names from the standard module math
from math import *
print("The value of pi is", pi)
Python Package
The packages in python facilitate the developer with the application development
environment by providing a hierarchical directory structure where a package contains
sub-packages, modules, and sub-modules.
A directory must contain a file named __init__.py in order for Python to consider it as a
package. This file can be left empty but we generally place the initialization code for that
package in this file.
Python File I/O
What is a file?
File is a named location on disk to store related information. It is used to permanently
store data in a non-volatile memory (e.g. hard disk).
When we want to read from or write to a file we need to open it first. When we are done,
it needs to be closed
Hence, in Python, a file operation takes place in the following order.
 Open a file
 Read or write (perform operation)
 Close the file
 How to open a file?
Python has a built-in function open() to open a file. This function returns a file object, also
called a handle, as it is used to read or modify the file accordingly.
>>> f = open("test.txt") # open file in current directory
>>> f = open("C:/folder/README.txt") # specifying full path
Python File I/O
 The default is reading in text mode. In this mode, we get strings when reading from
the file.
 On the other hand, binary mode returns bytes and this is the mode to be used when
dealing with non-text files like image or exe files.
Python File I/O
Python File I/O
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
f = open("test.txt",mode = 'r',encoding = 'utf-8')
 How to close a file Using Python
When we are done with operations to the file, we need to properly close the file.
Closing a file will free up the resources that were tied with the file and is done using Python
close() Function
Python File I/O
f = open("test.txt",encoding = 'utf-8')
# perform file operations
f.close()
 A safer way is to use a try...finally block.
try:
f = open("test.txt",encoding = 'utf-8')
# perform file operations
finally:
f.close()
Python File I/O
 How to write to File in Python
with open("test.txt",'w',encoding = 'utf-8') as f:
f.write("my first filen")
f.write("This filenn")
f.write("contains three linesn")
How to read files in Python
Python File I/O
>>> f = open("test.txt",'r',encoding = 'utf-8')
>>> f.read(4) # read the first 4 data
>>> f.read() # read in the rest till end of file
>>> f.tell() # get the current file position
>>> f.seek(0) # bring file cursor to initial position
Python File I/O
Python File I/O
T H A N K S
1 de 17

Recomendados

Python Session - 6 por
Python Session - 6Python Session - 6
Python Session - 6AnirudhaGaikwad4
308 visualizações21 slides
Python Session - 2 por
Python Session - 2Python Session - 2
Python Session - 2AnirudhaGaikwad4
208 visualizações14 slides
Python Session - 3 por
Python Session - 3Python Session - 3
Python Session - 3AnirudhaGaikwad4
282 visualizações21 slides
Python Session - 4 por
Python Session - 4Python Session - 4
Python Session - 4AnirudhaGaikwad4
377 visualizações27 slides
Python Presentation por
Python PresentationPython Presentation
Python PresentationNarendra Sisodiya
89.3K visualizações47 slides
Introduction to python por
Introduction to pythonIntroduction to python
Introduction to pythonMaheshPandit16
307 visualizações115 slides

Mais conteúdo relacionado

Mais procurados

Python (basic) por
Python (basic)Python (basic)
Python (basic)Sabyasachi Moitra
2.8K visualizações104 slides
Python revision tour i por
Python revision tour iPython revision tour i
Python revision tour iMr. Vikram Singh Slathia
5.5K visualizações37 slides
Python by Rj por
Python by RjPython by Rj
Python by RjShree M.L.Kakadiya MCA mahila college, Amreli
2K visualizações243 slides
Python Interview Questions For Freshers por
Python Interview Questions For FreshersPython Interview Questions For Freshers
Python Interview Questions For Fresherszynofustechnology
90 visualizações3 slides
Python 3 Programming Language por
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming LanguageTahani Al-Manie
11.5K visualizações57 slides
Introduction to Python por
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
36.8K visualizações62 slides

Mais procurados(20)

Python (basic) por Sabyasachi Moitra
Python (basic)Python (basic)
Python (basic)
Sabyasachi Moitra2.8K visualizações
Python Interview Questions For Freshers por zynofustechnology
Python Interview Questions For FreshersPython Interview Questions For Freshers
Python Interview Questions For Freshers
zynofustechnology90 visualizações
Python 3 Programming Language por Tahani Al-Manie
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
Tahani Al-Manie11.5K visualizações
Introduction to Python por amiable_indian
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian36.8K visualizações
Learn Python The Hard Way Presentation por Amira ElSharkawy
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy913 visualizações
Let’s Learn Python An introduction to Python por Jaganadh Gopinadhan
Let’s Learn Python An introduction to Python Let’s Learn Python An introduction to Python
Let’s Learn Python An introduction to Python
Jaganadh Gopinadhan3.6K visualizações
Python training por Kunalchauhan76
Python trainingPython training
Python training
Kunalchauhan763.7K visualizações
Python-01| Fundamentals por Mohd Sajjad
Python-01| FundamentalsPython-01| Fundamentals
Python-01| Fundamentals
Mohd Sajjad2.5K visualizações
Basic Concepts in Python por Sumit Satam
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
Sumit Satam4.3K visualizações
Fundamentals of Python Programming por Kamal Acharya
Fundamentals of Python ProgrammingFundamentals of Python Programming
Fundamentals of Python Programming
Kamal Acharya9.8K visualizações
PYTHON NOTES por Ni
PYTHON NOTESPYTHON NOTES
PYTHON NOTES
Ni 11.6K visualizações
Python Tutorial Part 1 por Haitham El-Ghareeb
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
Haitham El-Ghareeb3.8K visualizações
Python advance por Deepak Chandella
Python advancePython advance
Python advance
Deepak Chandella149 visualizações
First Steps in Python Programming por Dozie Agbo
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
Dozie Agbo202 visualizações
11 Unit 1 Chapter 02 Python Fundamentals por praveenjigajinni
11  Unit 1 Chapter 02 Python Fundamentals11  Unit 1 Chapter 02 Python Fundamentals
11 Unit 1 Chapter 02 Python Fundamentals
praveenjigajinni5.1K visualizações
Learn python – for beginners por RajKumar Rampelli
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
RajKumar Rampelli1.9K visualizações
Python interview questions for experience por MYTHILIKRISHNAN4
Python interview questions for experiencePython interview questions for experience
Python interview questions for experience
MYTHILIKRISHNAN443 visualizações

Similar a Python Session - 5

Chapter - 5.pptx por
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptxMikialeTesfamariam
20 visualizações27 slides
Introduction To Programming with Python-5 por
Introduction To Programming with Python-5Introduction To Programming with Python-5
Introduction To Programming with Python-5Syed Farjad Zia Zaidi
447 visualizações14 slides
Module2-Files.pdf por
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf4HG19EC010HARSHITHAH
4 visualizações23 slides
FILE HANDLING.pptx por
FILE HANDLING.pptxFILE HANDLING.pptx
FILE HANDLING.pptxkendriyavidyalayano24
12 visualizações60 slides
Python-files por
Python-filesPython-files
Python-filesKrishna Nanda
144 visualizações23 slides
Python-FileHandling.pptx por
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptxKarudaiyar Ganapathy
62 visualizações21 slides

Similar a Python Session - 5(20)

Introduction To Programming with Python-5 por Syed Farjad Zia Zaidi
Introduction To Programming with Python-5Introduction To Programming with Python-5
Introduction To Programming with Python-5
Syed Farjad Zia Zaidi447 visualizações
Python-files por Krishna Nanda
Python-filesPython-files
Python-files
Krishna Nanda144 visualizações
Python File Handling | File Operations in Python | Learn python programming |... por Edureka!
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
Edureka!7.1K visualizações
Python Files I_O17.pdf por RashmiAngane1
Python Files I_O17.pdfPython Files I_O17.pdf
Python Files I_O17.pdf
RashmiAngane118 visualizações
Chapter 08 data file handling por praveenjigajinni
Chapter 08 data file handlingChapter 08 data file handling
Chapter 08 data file handling
praveenjigajinni23.9K visualizações
File handling3.pdf por nishant874609
File handling3.pdfFile handling3.pdf
File handling3.pdf
nishant87460933 visualizações
File handling in Python por Megha V
File handling in PythonFile handling in Python
File handling in Python
Megha V7.2K visualizações
File handling4.pdf por sulekha24
File handling4.pdfFile handling4.pdf
File handling4.pdf
sulekha247 visualizações
DFH PDF-converted.pptx por AmitKaur17
DFH PDF-converted.pptxDFH PDF-converted.pptx
DFH PDF-converted.pptx
AmitKaur175 visualizações
Using Python Libraries.pdf por SoumyadityaDey
Using Python Libraries.pdfUsing Python Libraries.pdf
Using Python Libraries.pdf
SoumyadityaDey36 visualizações
Chapter 03 python libraries por praveenjigajinni
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
praveenjigajinni11.8K visualizações
File Handling as 08032021 (1).ppt por Raja Ram Dutta
File Handling as 08032021 (1).pptFile Handling as 08032021 (1).ppt
File Handling as 08032021 (1).ppt
Raja Ram Dutta19 visualizações

Último

Benefits in Software Development por
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software DevelopmentJohn Valentino
5 visualizações15 slides
The Era of Large Language Models.pptx por
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptxAbdulVahedShaik
7 visualizações9 slides
Introduction to Git Source Control por
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source ControlJohn Valentino
7 visualizações18 slides
What is API por
What is APIWhat is API
What is APIartembondar5
13 visualizações15 slides
Airline Booking Software por
Airline Booking SoftwareAirline Booking Software
Airline Booking SoftwareSharmiMehta
9 visualizações26 slides
aATP - New Correlation Confirmation Feature.pptx por
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptxEsatEsenek1
205 visualizações6 slides

Último(20)

Benefits in Software Development por John Valentino
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software Development
John Valentino5 visualizações
The Era of Large Language Models.pptx por AbdulVahedShaik
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptx
AbdulVahedShaik7 visualizações
Introduction to Git Source Control por John Valentino
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source Control
John Valentino7 visualizações
What is API por artembondar5
What is APIWhat is API
What is API
artembondar513 visualizações
Airline Booking Software por SharmiMehta
Airline Booking SoftwareAirline Booking Software
Airline Booking Software
SharmiMehta9 visualizações
aATP - New Correlation Confirmation Feature.pptx por EsatEsenek1
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptx
EsatEsenek1205 visualizações
360 graden fabriek por info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492165 visualizações
ADDO_2022_CICID_Tom_Halpin.pdf por TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 visualizações
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... por Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 visualizações
AI and Ml presentation .pptx por FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8714 visualizações
The Path to DevOps por John Valentino
The Path to DevOpsThe Path to DevOps
The Path to DevOps
John Valentino5 visualizações
How to build dyanmic dashboards and ensure they always work por Wiiisdom
How to build dyanmic dashboards and ensure they always workHow to build dyanmic dashboards and ensure they always work
How to build dyanmic dashboards and ensure they always work
Wiiisdom14 visualizações
Flask-Python.pptx por Triloki Gupta
Flask-Python.pptxFlask-Python.pptx
Flask-Python.pptx
Triloki Gupta9 visualizações
Page Object Model por artembondar5
Page Object ModelPage Object Model
Page Object Model
artembondar56 visualizações
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... por TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 visualizações
FOSSLight Community Day 2023-11-30 por Shane Coughlan
FOSSLight Community Day 2023-11-30FOSSLight Community Day 2023-11-30
FOSSLight Community Day 2023-11-30
Shane Coughlan7 visualizações
Quality Engineer: A Day in the Life por John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino7 visualizações
Playwright Retries por artembondar5
Playwright RetriesPlaywright Retries
Playwright Retries
artembondar56 visualizações
Automated Testing of Microsoft Power BI Reports por RTTS
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI Reports
RTTS10 visualizações

Python Session - 5

  • 1. Python Session - 5 Anirudha Anil Gaikwad gaikwad.anirudha@gmail.com
  • 2. What you learn  Python Modules  Python Package  Python File I/O
  • 3. Python Modules Modules refer to a file containing Python statements and definitions. A file containing Python code, for e.g.: filename.py, is called a module and its module name would be filename. We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.
  • 4. Python Modules  How to import modules in Python? use the import keyword to import modules in Python >>> import filename # import statement example # to import standard module math import math print("The value of pi is", math.pi)
  • 5. Python Modules  Import with renaming We can import a module by renaming it as follows. import calendar as cal print("------Calendar Program in Python------n"); print("Enter 'x' for exit."); y = input("Enter Year: "); if y == 'x': exit(); else: m = input("Enter month: "); yy = int(y); mm = int(m); print("n",cal.month(yy,mm))
  • 6. Python Modules  Python from...import statement from used to import specific names from a module without importing the module as a whole. # import only pi from math module from math import pi print("The value of pi is", pi)  Import all names # import all names from the standard module math from math import * print("The value of pi is", pi)
  • 7. Python Package The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.
  • 8. Python File I/O What is a file? File is a named location on disk to store related information. It is used to permanently store data in a non-volatile memory (e.g. hard disk). When we want to read from or write to a file we need to open it first. When we are done, it needs to be closed Hence, in Python, a file operation takes place in the following order.  Open a file  Read or write (perform operation)  Close the file
  • 9.  How to open a file? Python has a built-in function open() to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. >>> f = open("test.txt") # open file in current directory >>> f = open("C:/folder/README.txt") # specifying full path Python File I/O  The default is reading in text mode. In this mode, we get strings when reading from the file.  On the other hand, binary mode returns bytes and this is the mode to be used when dealing with non-text files like image or exe files.
  • 11. Python File I/O f = open("test.txt") # equivalent to 'r' or 'rt' f = open("test.txt",'w') # write in text mode f = open("img.bmp",'r+b') # read and write in binary mode f = open("test.txt",mode = 'r',encoding = 'utf-8')
  • 12.  How to close a file Using Python When we are done with operations to the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file and is done using Python close() Function Python File I/O f = open("test.txt",encoding = 'utf-8') # perform file operations f.close()  A safer way is to use a try...finally block. try: f = open("test.txt",encoding = 'utf-8') # perform file operations finally: f.close()
  • 13. Python File I/O  How to write to File in Python with open("test.txt",'w',encoding = 'utf-8') as f: f.write("my first filen") f.write("This filenn") f.write("contains three linesn")
  • 14. How to read files in Python Python File I/O >>> f = open("test.txt",'r',encoding = 'utf-8') >>> f.read(4) # read the first 4 data >>> f.read() # read in the rest till end of file >>> f.tell() # get the current file position >>> f.seek(0) # bring file cursor to initial position
  • 17. T H A N K S