FILE HANDLING
File handling is an integral part of programming.
File handling in Python is simplified by built-in methods,
which include creating, opening, and closing files.
While files are open, Python additionally allows performing
various file operations, such as reading, writing, and
appending information.
FILE HANDLING
Text files: In this type of file, Each line of text is terminated
with a special character called EOL (End of Line), which is the
new line character (‘\n’) in python by default.
Binary files: In this type of file, there is no terminator for a
line and the data is stored after converting it into machine-
understandable binary language.
OPENING FILES
The open() Python method is the primary file handling
function. The basic syntax is:
file_object = open('file_name', 'mode')
twt=open(‘tweets.txt’,’r’)
OPENING FILES
The open() function takes two elementary parameters for
file handling:
1. The file_name includes the file extension and assumes
the file is in the current working directory. If the file
location is elsewhere, provide the absolute or relative path.
2. The mode is an optional parameter that defines the file
opening method. The table below outlines the different
possible options:
OPENING FILES
CLOSING FILES
close() function closes the file and frees the
memory space acquired by that file. It is used at the
time when the file is no longer needed or if it is to
be opened in a different file mode.
file_object.close()
WRITING TO A FILE
There are 2 ways of writing to a file:
WRITING TO A FILE
READING FROM A FILE
After importing a file into an object, Python offers
numerous methods to read the contents.
Use the read() method on the file object and print the
result. For example:
file1 = open('myfile.txt', 'r')
print(file1.read())
file1.close()
READ PARTS OF A FILE
Provide a number to the read() function to read only
the specified number of characters
f = open("file.txt")
print(f.read(5))
READ FROM A FILE
Alternatively, use the readline() method to print only
the first line of the file:
READ FROM A FILE
f = open("file.txt")
print(f.readline())
READ FROM A FILE
f = open("file.txt")
print(f.readlines())
READ FROM A FILE
f = open("file.txt")
for line in f:
print(line, end="")
LINE COUNT
The readlines() function is most straightforward way to count
the number of lines in a text file in Python.
The readlines() method reads all lines from a file and stores it
in a list.
Next, use the len() function to find the length of the list which
is nothing but total lines present in a file.
Open a file and use the readlines() method on file pointer to
read all lines.
LINE COUNT
fp=open("myfile.txt","r")
list=fp.readlines()
lines=len(list)
print(lines)
WORD COUNT
text= open("example.txt", "rt")
page=text.read()
word=page.split()
print("Total words: ",len(word))
WORD OCCURANCE COUNT
def word_count(str):
counts = dict()
words = str.split()
for word in words:
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
f=open("myfile.txt","rt")
print( word_count(f.read()))