Opens in a new windowOpens an external websiteOpens an external website in a new window
We and our 19 IAB TCF partners store and access information on your device for the following purposes: store and/or access information on a device, advertising and content measurement, audience research, and services development, personalised advertising, and personalised content.
Personal data may be processed to do the following: use precise geolocation data and actively scan device characteristics for identification.
Our third party IAB TCF partners may store and access information on your device such as IP address and device characteristics. Our IAB TCF Partners may process this personal data on the basis of legitimate interest, or with your consent. You may change or withdraw your preferences at any time by clicking on the cookie icon or link; however, as a consequence, you may not see relevant ads or personalized content. To learn more, view the following link: Cookie Policy
The document provides an overview of file handling in Python, explaining the creation, reading, writing, and manipulation of both text and binary files. It describes basic operations such as opening, closing, and accessing files, as well as using various methods like read(), write(), and seek() for file processing. Additionally, it introduces the os module for file and directory management and discusses the importance of data interpretation when working with binary files.
A file(i.e. data file) is a named place on the disk
where a sequence of related data is stored. In
python files are simply stream of data, so the
structure of data is not stored in the file, along
with data.
Basic operations performed on a data file are:
Naming a file
Opening a file
Reading data from the file
Writing data in the file
Closing a file
3.
Using thesebasic operations, we can process file
in many ways, such as
Creating a file
Traversing a file for displaying the data on screen
Appending data in file
Inserting data in file
Deleting data from file
Create a copy of file
Updating data in the file, etc.
Python allow us to create and manage two types
of file
Text
Binary
4.
A textfile is usually considered as sequence
of lines. Line is a sequence of characters
(ASCII), stored on permanent storage media.
Although default character coding in python
is ASCII but using constant u with string,
supports Unicode as well.
Each line is terminated by a special character,
known as End of Line (EOL). From strings we
know that n is newline character. So at the
lowest level, text file will be collection of
bytes. Text files are stored in human readable
form and they can also be created using any
text editor
5.
A binaryfile contains arbitrary binary data i.e.
numbers stored in the file, can be used for
numerical operation(s). So when we work on
binary file, we have to interpret the raw bit
pattern(s) read from the file into correct type of
data in our program.
It is perfectly possible to interpret a stream of
bytes originally written as string, as numeric
value. But we know that will be incorrect
interpretation of data and we are not going to get
desired output after the file processing activity.
So in the case of binary file it is extremely
important that we interpret the correct data type
while reading the file. Python provides special
module(s) for encoding and decoding of data for
binary file.
6.
To handledata files in python, we need to have
a file object. Object can be created by using
open() function or file() function. To work on
file, first thing we do is open it. This is done by
using built in function open().
Using this function a file object is created
which is then used for accessing various
methods and functions available for file
manipulation.
Syntax of open() function is
file_object = open(filename [, access_mode]
[,buffering])
7.
open() requiresthree arguments to
work, first one ( filename ) is the
name of the file . The name can
include the description of path.
The second parameter (access_mode)
describes how file will be used
throughout the program. This is an
optional parameter and the default
access_mode is reading.
8.
The thirdparameter (buffering) is for specifying
how much is read from the file in one read. The
function will return an object of file type using
which we will manipulate the file, in our program.
When we work with file(s), a buffer (area in
memory where data is temporarily stored before
being written to file), is automatically associated
with file when we open the file. While writing the
content in the file, first it goes to buffer and once
the buffer is full, data is written to the file. Also
when file is closed, any unsaved data is
transferred to file. flush() function is used to
force transfer of data from buffer to file.
9.
File accessmodes
r will open the text file for reading only and rb will do
the same for binary format file.
w will open a text file for writing only and wb for
binary format file.
r+ will open a text file and rb+ will open a binary file,
for both reading and writing purpose. The file pointer
is placed at the beginning of the file when it is
opened using r+ / rb+ mode.
w+ opens a file in text format and wb+ in binary
format, for both writing and reading. File pointer will
be placed at the beginning for writing into it, so an
existing file will be overwritten. A new file can also be
created using this mode.
a+ opens a text file and ab+ opens a binary file, for
both appending and reading. File pointer is placed at
the end of the file, in an already existing file. Using
this mode a non existing file may be created.
10.
fileobject. close()will be used to close the file
object, once we have finished working on it.
readline() will return a line read, as a string
from the file
fileobject.readline()
Since the method returns a string it's usage
will be
>>>x = file.readline()
or
>>>print file.readline()
11.
readlines()can beused to read the entire
content of the file. You need to be careful
while using it w.r.t. size of memory required
before using the function. The method will
return a list of strings, each separated by
n.
An example of reading entire data of file in
list is:
It's syntax is:
fileobject.readlines()
as it returns a list, which can then be used for
manipulation.
12.
read() canbe used to read specific size string
from file. This function also returns a string read
from the file.
At the end of the file, again an empty string will
be returned.
Syntax of read() function is
fileobject.read([size])
Here size specifies the number of bytes to be
read from the file.
lines = []
content = file.read() # since no size is given,
entire file will be read
lines = content.splitlines()
print lines
will give you a list of strings:
['hello world.', 'this is my first file handling
program.', 'I am using python language.']
13.
For sendingdata in file, i.e. to create / write
in the file, write() and writelines() methods
can be used.
write() method takes a string ( as parameter )
and writes it in the file. For storing data with
end of line character, you will have to add n
character to end of the string.
Notice addition of n in the end of every
sentence while talking of data.txt. As
argument to the function has to be string, for
storing numeric value, we have to convert it
to string.
14.
Its syntaxis
fileobject.write(string)
Example
>>>f = open('test1.txt','w')
>>>f.write("hello worldn")
>>>f.close()
For numeric data value conversion to string is
required.
Example
>>>x = 52
>>>file.write(str(x))
15.
For writinga string at a time, we use write()
method, it can't be used for writing a list, tuple
etc. into a file.
Sequence data type can be written using
writelines() method in the file. It's not that, we
can't write a string
using writelines() method.
It's syntax is:
fileobject.writelines(seq)
So, whenever we have to write a sequence of
string / data type, we will use writelines(), instead
of write().
f = open('test2.txt','w')
str = 'hello world.n this is my first file handling
program.n I am using python language"
f.writelines(str)
f.close()
16.
To accessthe contents of file randomly - seek and tell
methods are used.
tell() method returns an integer giving the current position
of object in the file. The integer returned specifies the
number of bytes from the beginning of the file till the
current position of file object.
It's syntax is
fileobject.tell()
seek()method can be used to position the file object at
particular place in the file. It's syntax is :
fileobject.seek(offset [, from_what])
here offset is used to calculate the position of fileobject in
the file in bytes. Offset is added to from_what (reference
point) to get the position. Following is the list of
from_what values:
Value reference point
0 beginning of the file
1 current position of file
2 end of file
default value of from_what is 0, i.e. beginning of the file.
17.
So forstoring data in binary format, we will use
pickle module.
First we need to import the module. It provides
two main methods for the purpose, dump and
load. For
creation of binary file we will use pickle.dump()
to write the object in file, which is opened in
binary access mode.
Syntax of dump() method is:
dump(object, fileobject)
Example:
def fileOperation1():
import pickle
l = [1,2,3,4,5,6]
file = open('list.dat', 'wb') # b in access mode is
for binary file
pickle.dump(l,file) # writing content to binary file
file.close()
22.
Files arealways stored in current folder / directory by
default. The os (Operating System) module ofpython
provides various methods to work with file and folder
/ directories. For using these functions, we have to
import os module in our program.
Some useful methods, which can be used with files in
os module are as follows:
path.abspath(filename) will give us the complete path
name of the data file.
path.isfile(filename) will check, whether the file exists
or not.
remove(filename) will delete the file. Here filename
has to be the complete path of file.
rename(filename1,filename2) will change the name of
filename1 with filename2.
23.
Once thefile is opened, then using file object,
we can derive various information about file.
This is done using file attributes defined in os
module. Some of the attributes defined in it
are
1. file.closed returns True if file is closed
2. file.mode returns the mode, with which file
was opened.
3. file.name returns name of the file
associated with file object while opening the
file.
24.
os.chdir(path) Changethe current working
directory to path.
os.getcwd() Return a string representing the
current working directory.
os.chmod(path, mode) Change the mode of
path to the numeric mode. mode may take
one of the following values (as defined in the
stat module) or bitwise ORed combinations of
them:
os.listdir(path) Return a list containing the
names of the entries in the directory given by
path. The list is in arbitrary order. It does not
include the special entries '.' and '..' even if
they are present in the directory.
25.
s.mkdir(path[, mode])Create a directory named path
with numeric mode mode. The default mode is 0777
(octal). On some systems, mode is ignored. Where it
is used, the current umask value is first masked out.
If the directory already exists, OSError is raised.
os.rmdir(path) Remove (delete) the directory path.
Only works when the directory is empty, otherwise,
OSError is raised. In order to remove whole directory
trees, shutil.rmtree() can be used.
os.rename(src, dst) Rename the file or directory src to
dst. If dst is a directory, OSError will be raised. stems.
If successful, the renaming will be an atomic
operation (this is a POSIX requirement).
On Windows, if dst already exists, OSError will be
raised even if it is a file; there may be no way to
implement an atomic rename when dst names an
existing file.
27.
def fileHandling():
file = open("story.txt","w+") # both reading &
writing
choice = True
while True:
line = raw_input("enter sentence :")
file.write(line) # creation of file
choice = raw_input("want to enter more data in
file Y / N")
if choice == 'N' : break
file.seek(0) # transferring file object to beginning
of the file
lines = file.readlines()
file.close()
for l in lines:
print l