Pages

Friday, July 24, 2015

Python - File Management

A File is a chunk of logically related data or information which can be used by computer programs.

Generally every Programming language provides the ability to store and retrieve information from file. The most basic tasks involving files will be reading and writing data.

Python also provides facilities for manipulating files. The logic for file management is provided directly without any additional functions or modules needed ( unlike in other programming language where additional modules are imported to work with files)

The syntax for reading and writing files in Python is similar to programming languages like C and C++ or Perl, but easier to handle. 

Open - In order to perform basic operations on files we need to first open it. We use the Open() function for opening the files after which creates  a file Object that is used to call other support methods associated with that.

The syntax is –
File Object = open(“File Name”, access mode, buffering)

File Name – Name argument is a String value that contains the name of the file being opened.

Access Mode – The access mode tells the mode in which is file is opened. File modes include read, write, append etc. The access mode is optional and the default one is read

Buffering – Buffering of the file contents is disabled if that is set to 0 and enabled when used 1. When a value more than 1 is used, buffering takes place with that value.
If a negative value is defined the default is used. This is an options value

The basic file modes are

R        – Open file for read only.
Rb      – open file for reading in binary more.
R+      - open file for both reading and writing
Rb+    - open files for reading and writing in binary mode
W       – Open file in write mode
W+     - Open file in read and write mode
Wb     – write in binary mode
A        - Append
A+      - Both  for appending and reading

File Objects - Now once the file is opened, the file object does have attributes that can be used to get information regarding the file. The details include

file.closed      Returns true if file is closed, false otherwise.
file.mode       Returns access mode with which file was opened.
file.name       Returns name of the file.


An Example – Now lets see an Example of Opening a File,

>>> fo =open("hunter.txt","wb")
>>> print fo.name
hunter.txt
>>> print fo.closed
False
>>> print fo.mode
wb


Writing to File -
>>> fo = open("hunter.txt","wb")
>>> fo.write("this is Python")
>>> fo.close()
>>> 

[djas999@vx181d imp]$ cat hunter.txt
this is Python

Reading from a File -
>>> fo = open("hunter.txt","r+")
>>> fo.read(10)
'this is Py'

Now the above read and write methods do the same thing as the name says. The read() method takes an integer value as argument which is the number of bytes to read. This starts from file and read bytes specified in file.

Closing – a method close() can be used to close the file

File Positions - When Ever we start reading a file , the file pointer starts from first and then moves on. Some times we may need to find out the pointer location or want to move the pointer to a specific location.

Tell() – The tell() method tells you the current position within the file.
Seek(offset, from) – This method changes the current file position. The offset indicates the number of bytes to be moved.  The from argument specifies the reference position from where the bytes are to be moved.

If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position as the reference position and if it is set to 2 then the end of the file would be taken as the reference position.

Some more examples include,

To read one line at a time, use:
fh = open("hello".txt", "r")
print fh.readline()

To read a list of lines use:
fh = open("hello.txt.", "r")
print fh.readlines()

To append to file, use:
fh = open("Hello.txt", "a")
write("Hello World again")
fh.close

To close a file, use
fh = open("hello.txt", "r")
print fh.read()

fh.close()

1 comment :

  1. Trying to take your recreation to the following degree?

    ReplyDelete