File Processing

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

FILE PROCESSING

Introduction:
• When a program is terminated, the entire data is lost. Storing in a file
will preserve your data even if the program terminates.
• Python provides us with an important feature for reading data from
the file and writing data into a file. Mostly, in programming
languages, all the values or data are stored in some variables which
are volatile in nature.
• Python has a built-in a=open(“abc.txt”,”x”) function to open a file.
This function returns a file object, also called a handle, as it is used to
read or modify the file accordingly.
Introduction:
• Python treats file differently as text or binary and this is important.
Each line of code includes a sequence of characters and they form
text file.
• Each line of a file is terminated with a special character, called the EOL
or End of Line characters like comma {,} or newline character.
• It ends the current line and tells the interpreter a new one has
begun..
File Operations:
• There are different operations that can be carried out on a file, these
are:
1) Creation of a new file
2) Opening an existing file
3) Reading from a file t.read()
4) Writing to a file r.write(“abc”)
5) Closing a file
Open() function:
• Open () function in Python to open a file in read or write mode.
syntax: open(Directory:\\filename, mode)
• There are various kinds of mode, that python provides:
Open() function:
Creation of a new file:

• To create a new file in Python, use the open() function, with one of
the following parameters:

• "x" - Create - will create a file, returns an error if the file exist

• "a" - Append - will create a file if the specified file does not exist

• "w" - Write - will create a file if the specified file does not exist
Creation of a new file:
• Example:
Create a file called "myfile.txt":
f = open("myfile.txt", "x")
• Result:
a new empty file is created!
• Example:
Create a new file if it does not exist:
f = open("myfile.txt", "w")
f.write("Now the file has more content!")
• Example:
Create a new file if it does not exist:
f = open("myfile.txt", “a")
f.write("Now the file has more content!")
Opening and Existing File and Reading
from the file
• Assume we have the following file, located in the same folder as
Python:
• File Name: demofile.txt
• Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
• To open the file, use the built-in open() function.
• The open() function returns a file object, which has a read() method
for reading the content of the file:
Opening and Existing File and Reading
from the file:
• Example:
f = open("demofile.txt", "r")
print(f.read())

• Output:
C:\Users\My Name>python demo_file_open.py
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
Opening and Existing File and Reading
from the file:
• If the file is located in a different location, you will have to specify the
file path, like this:
• Example:
Open a file on a different location:
f = open("D:\\myfiles\welcome.txt", "r")
print(f.read())
Read Only Parts of the File:

• By default the read() method returns the whole text, but you can also
specify how many characters you want to return:
• Example:
Return the 5 first characters of the file:
f = open("demofile.txt", "r")
print(f.read(5))
• Output:
C:\Users\My Name>python demo_file_open2.py
Hello
Read Lines:

• You can return one line by using the readline() method:


• Example:
Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())1
print(f.readline())2
print(f.readline())3

• Output:
C:\Users\My Name>python demo_file_readline.py
Hello! Welcome to demofile.txt
Read Lines using Loop:
• By looping through the lines of the file, you can read the whole file, line by line:
• Example:
Loop through the file line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
• Output:
C:\Users\My Name>python demo_file_readline3.py
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!
Close Files:

• It is a good practice to always close the file when you are done with it.
• Example:
Close the file when you are finish with it:
f = open("demofile.txt", "r")
print(f.readline())
f.close()

• Output:
C:\Users\My Name>python demo_file_close.py
Hello! Welcome to demofile.txt

You might also like