File Handling:-
- Programming Languages are good at processing the data, But they cannot store the data permanently.
- Because programming Languages programs memory will be allocated in RAM.
- RAM is a volatile so that once the program execution is over the memory which is allocated for that program is going to be de-allocated.
- If we don't store the data permanently we cannot able to store the data in future.
- In order to store the data permanently we use Files, RDBMS or ORDBMS(Object relational database management system), NOSQL.
- File is a named location on the disk to store related information.It is used to permanently store data in a non-volatile memory (e.g. hard disk). Since, random access memory (RAM) is volatile which loses its data when computer is turned off, we use files for future use of the data.
- Python Language is providing API or Libraries to perform the operations on the files.
- Before going to perform the read/write operations on the files first we have to open the files. We can open the files by calling the open function of builtins module.
- At the time of opening the file we need to specify the mode of the file
MODE
|
DESCRIPTION
|
r
|
Opens the file
for reading (default)
|
w
|
Opens the file in
writing. Creates a new file if it does not exist or truncate the file if it
exists.
|
x
|
Opens the file in
exclusive creation. If the file already exists, the operation fails.
|
a
|
Open for
appending at the end of the file. Creates a new file of it does not exists.
|
t
|
Opens in text
mode(Default)
|
b
|
Opens in binary
mode.
|
+
|
Opens file for
updating(Reading and Writing)
|
- After executing the open function it returns file object.
- File Object provides some methods to read or write the data to the files.
- After performing the operations on the file we have to close the file.
- We can close the file by calling close( ) method on the file Object.
in Python, a file
operation takes place in the following order.
1.
Open a file
2.
Read or write (perform operation)
3.
Close the file
Opening a File:
Python has a built-in
function open( ) 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.
f = open("test.txt") # open file in current directory
f = open("C:/Python33/README.txt")
# specifying full path
f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
Closing a File:
- When we are done with operations to the file, we need to properly close it. Python has a garbage collector to clean up un-referenced objects. But we must not rely on it to close the file. Closing a file will free up the resources that were tied with the file and is done using the close( ) method.
f = open("test.txt")
f.close()
Reading data from the file:
x=open("test.txt")
print(x.read( ))
x.close( )
Writing data to file:
x=open("z.txt","w")
x.write("Welcome")
x.close( )
Appending data to file:
x=open("z.txt","a")
x.write("Python")
x.close( )
Python
File Methods
|
|
Method
|
Description
|
close()
|
Close an open file. It has no effect if the file is
already closed.
|
detach()
|
Separate the underlying binary buffer from the TextIOBaseand return it.
|
fileno()
|
Return an integer number (file descriptor) of the file.
|
flush()
|
Flush the write buffer of the file stream.
|
isatty()
|
Return True if the file stream is
interactive.
|
read(n)
|
Read at most n characters
form the file. Reads till end of file if it is negative or None.
|
readable()
|
Returns True if the file stream can be
read from.
|
readline(n=-1)
|
Read and return one line from the file. Reads in at most nbytes if specified.
|
readlines(n=-1)
|
Read and return a list of lines from the file. Reads in
at most n bytes/characters if
specified.
|
seek(offset,from=SEEK_SET)
|
Change the file position to offset bytes, in reference tofrom (start, current, end).
|
seekable()
|
Returns True if the file stream
supports random access.
|
tell()
|
Returns the current file location.
|
truncate(size=None)
|
Resize the file stream to size bytes.
If size is not specified, resize
to current location.
|
writable()
|
Returns True if the file stream can be
written to.
|
write(s)
|
Write string s to
the file and return the number of characters written.
|
writelines(lines)
|
Write a list of lines to the
file.
|
Working with file methods:
x=open("z.txt")
print(x.tell( ))
print(x.read(3))
print(x.tell( ))
print(x.read(4))
print(x.tell( ))
x.seek(5)
print(x.tell( ))
print(x.read(10))
print(x.tell( ))
x.close( )
working "with" statement:
with open('test.txt') as f1:
print(f1.read())
f1.close()
working "with" statement:
with open('test.txt') as f1:
print(f1.read())
f1.close()
No comments:
Post a Comment