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)

DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
PPTX
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Introduction to Probability(basic) .pptx
purohitanuj034
 
PPTX
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Basics and rules of probability with real-life uses
ravatkaran694
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
LDP-2 UNIT 4 Presentation for practical.pptx
abhaypanchal2525
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
ENGLISH 8 WEEK 3 Q1 - Analyzing the linguistic, historical, andor biographica...
OliverOllet
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Introduction to Probability(basic) .pptx
purohitanuj034
 
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
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