Hello and welcome to this lecture of our course.
open files in python
We should open files in python before we can read or write.
So could you open function in Python to open files that defied equal of of embedded PCs of double codes.
open double quotes File underscore name
close double quotes comma double quotes open mode close double quotes
open moods are default only open modes R rb binary read and write mode
R B plus finally read and write w default write only mode wb binary write only and w+
read and write default mode wb+ is binary read and write B binary mode a append read and write
create the file if the file not exists.
X creates the file if the file not exists mode.
file.read( ):
We use this method to read over the file or lines of the file f equal open parentheses
file underscore name double quotes comma double quotes open mode close double quotes
double quotes open mode close double quotes close parenthesis f equal open open parenthesis open double quotes file_name dote text close parenthesis and r
and r print f dote print
f dote read 6 print f dote read 10
Then we will read and print the first two or six or ten lines for our file.
Find dote read line.
We use this method to read the file line by line f equal open file_name and open mode.
f equal open
file_name.text and r print f.readline ( )
loop file function we use with open file
Function to read our file f equal open file_name and open mode
f equal open file_name.txt and r for line in file print line
if.close( ) we use this method to close the file which is a good practice to avoid system memory block by the files we opened
f equal open ("file_name", "open mode")
f equal open("file_name.txt", "r") print f dote readline( ) f dote close( )
to remove the file we have opened and closed as full data underscore file equal open
("file_name","open mode")
f equal open("file_name.txt", "r")
print(f.readline( ))
f dote close( )
import os os.remove("file_name.txt")
so we can move our file as this method from opening to close to remove if the file is a file isn't exist
we will get Error
Thank you for watching.
Hope you enjoy this course. Don't forget to review this course.