FILES CONCEPTS IN PYTHON PROGRAMMING.pptx

shalinikarunakaran1 77 views 24 slides Sep 30, 2024
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

FILES IN PYTHON PROGRAMMING


Slide Content

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 4 th 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
Tags