KEMBAR78
FILES CONCEPTS IN PYTHON PROGRAMMING.pptx
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

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 isvery 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 indaily life A file in the computer hard disk
  • 4.
    TYPES OF FILESIN 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 fromfile #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 Thisis my first line If program 2, we use the read() method as: str = f.read(4)
  • 9.
    WORKING WITH TEXTFILES 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 !=‘@’ #writethe 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 afile 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 ismyfile line one this is line two @
  • 14.
    Now to readthe 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 frona 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 filecontents are: this is my file line one this is line two
  • 17.
    KNOWING WHETHER AFILE 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:’) ifos.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:pythoncheck.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 WITHDIRECTORIES  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 PROGRAMSFROM 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 Volumein drive f is new volume Volume serial number is8683-5094