SlideShare a Scribd company logo
FILES IN PYTHON
 FILES
 TYPES OF FILES IN PYTHON
 OPENING A FILE
 CLOSING A FILE
 WORKING WITH TEXT FILES CONTAING STRING
 KNOWING WHETHER A FILE EXIST OR NOT
 THE SEEK() AND TELL() METHODS
 RANDOM ACCESS OF BINARY FILES
FILES:
 Data is very important
 To store data in a computer
 A file is nothing but collection of data that is available in data
 The file in our daily life and the file stored in computer
 when data stored in a file it is permanently stored
A file in daily life A file in the computer hard disk
TYPES OF FILES IN PYTHON:

In python contains two types:
 Text files
 Binary files
 Binary files stored entire data in the form of bytes
 Image files are generally available in jpg,gif, or png formats
 Binary files can be used to store text,image,audio and video
OPENING A FILE:
 We should use open() function to open a file
File handler=open(“file name” ,“open mode”, ” buffering”)
 The file modeopen mode represent the purpose of opening
 A buffer reprsents the temporary block
f=open(“myfile.txt”, “w”)
CLOSING A FILE
 This happens when we are working with several files
f . close()
 The file represented by the object ’f’
 We are create a file we want to store some character
 We store the string into write() method
f . Write()
EXAMPLE:
#reading characters from file
#open the file for reading data
F=open(‘my file.txt’ ,’r’)
Str=f.read()
Print(str)
#closing the file
f.close()
 OUTPUT:
C:>python read.py
This is my first line
If program 2, we use the read() method as:
str = f.read(4)
WORKING WITH TEXT FILES CONTAINING
STRINGS
 To store a group of strings into a text file
 Please observe the”n” at the end of the string inside write()method to
write() method inside all the strings into file as long as user
 We can use the while loop as:
EXAMPLE:
While str !=‘@’
#write the string into file
If (str!=‘@’
f.write(str+”n”)
 The write() metod writes all the string seguentially in the single line to
write a string in ifferent lines we supposed to add “n” character at the
end of each string
 This method reads all the lines of the text files and displays them line by
line as they were strored in the “myfile.txt”
# creating a file with strings
#opening the file for writing data
F=open(‘myfile.txt’,’w’)
#enter strings fron keyboard
Print(‘enter text (@ at end):)
While str!=‘@’:
Str=input()
If(str!=‘@’)
F.write(str+”n”)
F.close()
OUTPUT:
c:>python create1.py
this is myfile line one
this is line two
@
Now to read the strings from the’myfile.txt’,we can use the read() method in the
following way
f.read()
There is another method by the names readlines() that reads all the lines into list
This is used as:
f.readline()
Wecan use read() method with splitlines method as:
f.read().splitlines()
CODING:
#readings strings fron a file
#open the file
F=open(‘myfile.txt’,’r’)
#read strings from the file
Print(‘the file contents are:)
Str=f.read()
Print(str)
f.close()
Output:
c:>python readl.py
the file contents are:
this is my file line one
this is line two
KNOWING WHETHER A FILE EXITS OR NOT:
The operatingsystem(os) module has a sub module by the name’path’ that
contains a method isfile() this method can be used to know whether a file that
we are operating really exist or not
F=open(fname,’r’)
Else:
Print(fname+’does not exist’)
Sys.exit()
CODING:
import os,sys
fname=input(‘enter filename:’)
if os.path.is file(fname)
else:
print(fname=‘does not exit’)
sys.exist()
print(‘the file contents are:’)
str=f.read()
f.close()
OUTPUT:
 OUTPUT:
 C:python check.py
 Enter the file name:yourfile.txt
 Yourfile.txt does no exist
 THE SEEK() AND TELL() METHOD
 We knows the data in binary files is stored in the form of bytes
 It returns the current position on the file pointer from the beginning method
of the file
n=f.tell()
 We will move the file pointer to the 4th
byte using seek() as:
 f.seek(3)
 WORKING WITH DIRECTORIES
 The os module represent operating system dependent functionality
 Let assume we are working with following dicrectory
import os
#get current working directory
current = os.getcwd()
print(current directory=,current)
OUTPUT:
c:>python.py
currentdirectory
9
RUNNING OTHER PROGRAMS FROM
PYTHON PROGRAM
 The module has thesystem() method that is useful to run an execution
program from our python program
 This method is similar to system() function of c langage example
import os
# execute dir command of dos operating system
os.system(‘dir*.py)
 OUTPUT:
F:py>python os.py
Volume in drive f is new volume
Volume serial number is8683-5094
FILES CONCEPTS IN PYTHON PROGRAMMING.pptx

More Related Content

Similar to FILES CONCEPTS IN PYTHON PROGRAMMING.pptx (20)

PPTX
Chapter 08 data file handling
Praveen M Jigajinni
 
PPTX
file handlling in python 23.12.24 geetha.pptx
vlkumashankardeekshi th
 
PPTX
Data File Handling in Python Programming
gurjeetjuneja
 
PDF
File handling with python class 12th .pdf
lionsconvent1234
 
PDF
FileHandling2023_Text File.pdf CBSE 2014
JAYASURYANSHUPEDDAPA
 
PPTX
Files in Python.pptx
Koteswari Kasireddy
 
PPTX
Files in Python.pptx
Koteswari Kasireddy
 
PDF
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
PPTX
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
PPTX
01 file handling for class use class pptx
PreeTVithule1
 
PPTX
Unit V.pptx
ShaswatSurya
 
PPTX
pspp-rsk.pptx
ARYAN552812
 
PPTX
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
PPTX
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
PPTX
file handling in python using exception statement
srividhyaarajagopal
 
PPTX
Python files / directories part16
Vishal Dutt
 
PPTX
file_handling_python_bca_computer_python
hansibansal
 
PPTX
File handling & basic operation on file in python
ishantjaiswal2004
 
PDF
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
Chapter 08 data file handling
Praveen M Jigajinni
 
file handlling in python 23.12.24 geetha.pptx
vlkumashankardeekshi th
 
Data File Handling in Python Programming
gurjeetjuneja
 
File handling with python class 12th .pdf
lionsconvent1234
 
FileHandling2023_Text File.pdf CBSE 2014
JAYASURYANSHUPEDDAPA
 
Files in Python.pptx
Koteswari Kasireddy
 
Files in Python.pptx
Koteswari Kasireddy
 
CHAPTER 2 - FILE HANDLING-txtfile.pdf is here
sidbhat290907
 
FILE HANDLING in python to understand basic operations.
ssuser00ad4e
 
01 file handling for class use class pptx
PreeTVithule1
 
Unit V.pptx
ShaswatSurya
 
pspp-rsk.pptx
ARYAN552812
 
UNIT –5.pptxpython for engineering students
SabarigiriVason
 
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
file handling in python using exception statement
srividhyaarajagopal
 
Python files / directories part16
Vishal Dutt
 
file_handling_python_bca_computer_python
hansibansal
 
File handling & basic operation on file in python
ishantjaiswal2004
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 

More from shalinikarunakaran1 (17)

PPTX
INTRODUCTION TO KEY FRAME SYSTEMS IN COMPUTER GRAPHICS
shalinikarunakaran1
 
PPTX
INTRODUCTION TO CMY COLOR MODELS IN COMPUTER GRAPHICS
shalinikarunakaran1
 
PPTX
COLOR MODELS IN RGB COLOR (INTRODUCTION)
shalinikarunakaran1
 
PPT
Data Communication and Networking ( Guided and Unguided Media)
shalinikarunakaran1
 
PPTX
Character generation
shalinikarunakaran1
 
PPTX
Character attributes
shalinikarunakaran1
 
PPTX
Attributes of output primitives( curve attributes & area fill attributes)
shalinikarunakaran1
 
PPTX
Attributes of output primitive(line attributes)
shalinikarunakaran1
 
PPTX
Video display devices
shalinikarunakaran1
 
PPTX
Raster scan system & random scan system
shalinikarunakaran1
 
PPTX
Introduction , surveuy on cg
shalinikarunakaran1
 
PPTX
Java tokens
shalinikarunakaran1
 
PPTX
Java program structure
shalinikarunakaran1
 
PPTX
Simple java program
shalinikarunakaran1
 
PPTX
How java differs from c and c++
shalinikarunakaran1
 
PPTX
JAVA HISTORY
shalinikarunakaran1
 
PPTX
JAVA FEATURES
shalinikarunakaran1
 
INTRODUCTION TO KEY FRAME SYSTEMS IN COMPUTER GRAPHICS
shalinikarunakaran1
 
INTRODUCTION TO CMY COLOR MODELS IN COMPUTER GRAPHICS
shalinikarunakaran1
 
COLOR MODELS IN RGB COLOR (INTRODUCTION)
shalinikarunakaran1
 
Data Communication and Networking ( Guided and Unguided Media)
shalinikarunakaran1
 
Character generation
shalinikarunakaran1
 
Character attributes
shalinikarunakaran1
 
Attributes of output primitives( curve attributes & area fill attributes)
shalinikarunakaran1
 
Attributes of output primitive(line attributes)
shalinikarunakaran1
 
Video display devices
shalinikarunakaran1
 
Raster scan system & random scan system
shalinikarunakaran1
 
Introduction , surveuy on cg
shalinikarunakaran1
 
Java tokens
shalinikarunakaran1
 
Java program structure
shalinikarunakaran1
 
Simple java program
shalinikarunakaran1
 
How java differs from c and c++
shalinikarunakaran1
 
JAVA HISTORY
shalinikarunakaran1
 
JAVA FEATURES
shalinikarunakaran1
 
Ad

Recently uploaded (20)

PPTX
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Nutri-QUIZ-Bee-Elementary.pptx...................
ferdinandsanbuenaven
 
PPT on the Development of Education in the Victorian England
Beena E S
 
How to Configure Prepayments in Odoo 18 Sales
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
1, 2, 3… E MAIS UM CICLO CHEGA AO FIM!.pdf
Colégio Santa Teresinha
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Capitol Doctoral Presentation -July 2025.pptx
CapitolTechU
 
Ad

FILES CONCEPTS IN PYTHON PROGRAMMING.pptx

  • 1. FILES IN PYTHON  FILES  TYPES OF FILES IN PYTHON  OPENING A FILE  CLOSING A FILE  WORKING WITH TEXT FILES CONTAING STRING  KNOWING WHETHER A FILE EXIST OR NOT  THE SEEK() AND TELL() METHODS  RANDOM ACCESS OF BINARY FILES
  • 2. FILES:  Data is very important  To store data in a computer  A file is nothing but collection of data that is available in data  The file in our daily life and the file stored in computer  when data stored in a file it is permanently stored
  • 3. A file in daily life A file in the computer hard disk
  • 4. TYPES OF FILES IN PYTHON:  In python contains two types:  Text files  Binary files  Binary files stored entire data in the form of bytes  Image files are generally available in jpg,gif, or png formats  Binary files can be used to store text,image,audio and video
  • 5. OPENING A FILE:  We should use open() function to open a file File handler=open(“file name” ,“open mode”, ” buffering”)  The file modeopen mode represent the purpose of opening  A buffer reprsents the temporary block f=open(“myfile.txt”, “w”)
  • 6. CLOSING A FILE  This happens when we are working with several files f . close()  The file represented by the object ’f’  We are create a file we want to store some character  We store the string into write() method f . Write()
  • 7. EXAMPLE: #reading characters from file #open the file for reading data F=open(‘my file.txt’ ,’r’) Str=f.read() Print(str) #closing the file f.close()
  • 8.  OUTPUT: C:>python read.py This is my first line If program 2, we use the read() method as: str = f.read(4)
  • 9. WORKING WITH TEXT FILES CONTAINING STRINGS  To store a group of strings into a text file  Please observe the”n” at the end of the string inside write()method to write() method inside all the strings into file as long as user  We can use the while loop as:
  • 10. EXAMPLE: While str !=‘@’ #write the string into file If (str!=‘@’ f.write(str+”n”)
  • 11.  The write() metod writes all the string seguentially in the single line to write a string in ifferent lines we supposed to add “n” character at the end of each string  This method reads all the lines of the text files and displays them line by line as they were strored in the “myfile.txt”
  • 12. # creating a file with strings #opening the file for writing data F=open(‘myfile.txt’,’w’) #enter strings fron keyboard Print(‘enter text (@ at end):) While str!=‘@’: Str=input() If(str!=‘@’) F.write(str+”n”) F.close()
  • 13. OUTPUT: c:>python create1.py this is myfile line one this is line two @
  • 14. Now to read the strings from the’myfile.txt’,we can use the read() method in the following way f.read() There is another method by the names readlines() that reads all the lines into list This is used as: f.readline() Wecan use read() method with splitlines method as: f.read().splitlines()
  • 15. CODING: #readings strings fron a file #open the file F=open(‘myfile.txt’,’r’) #read strings from the file Print(‘the file contents are:) Str=f.read() Print(str) f.close()
  • 16. Output: c:>python readl.py the file contents are: this is my file line one this is line two
  • 17. KNOWING WHETHER A FILE EXITS OR NOT: The operatingsystem(os) module has a sub module by the name’path’ that contains a method isfile() this method can be used to know whether a file that we are operating really exist or not F=open(fname,’r’) Else: Print(fname+’does not exist’) Sys.exit()
  • 18. CODING: import os,sys fname=input(‘enter filename:’) if os.path.is file(fname) else: print(fname=‘does not exit’) sys.exist() print(‘the file contents are:’) str=f.read() f.close() OUTPUT:
  • 19.  OUTPUT:  C:python check.py  Enter the file name:yourfile.txt  Yourfile.txt does no exist
  • 20.  THE SEEK() AND TELL() METHOD  We knows the data in binary files is stored in the form of bytes  It returns the current position on the file pointer from the beginning method of the file n=f.tell()  We will move the file pointer to the 4th byte using seek() as:  f.seek(3)
  • 21.  WORKING WITH DIRECTORIES  The os module represent operating system dependent functionality  Let assume we are working with following dicrectory import os #get current working directory current = os.getcwd() print(current directory=,current) OUTPUT: c:>python.py currentdirectory 9
  • 22. RUNNING OTHER PROGRAMS FROM PYTHON PROGRAM  The module has thesystem() method that is useful to run an execution program from our python program  This method is similar to system() function of c langage example import os # execute dir command of dos operating system os.system(‘dir*.py)
  • 23.  OUTPUT: F:py>python os.py Volume in drive f is new volume Volume serial number is8683-5094