Thursday, 9 April 2020

File Attributes and Stream In File Handling (Python)




FILE I/O ATTRIBUTES

Various attributes are related to file when it is opened or closed.
Those attributes are explained in the table:

Attribute
Description
name
Returns the name of the file (Including path)
mode
Returns mode of the file. (r or w etc.)
encoding
Returns the encoding format of the file
closed
Returns True if the file closed else returns False

For Example:
f = open("D:\\story.txt", "r")
print("Name of the File: ", f.name)
print("\n File-Mode : ", f.mode)
print("File encoding format : ", f.encoding)
print("Is File closed? ", f.closed)
f.close()
print("Is File closed? ", f.closed)
OUTPUT:
Name of the File: D:\story.txt
File-Mode : r
File encoding format : cp1252
Is File closed? False
Is File closed? True


Relative and Absolute Paths :
We all know that the files are kept in directory which are also known as folders.
 Every running program has a current directory. Which is generally a default directory and python always see the default directory first.
The absolute paths are from the topmost level of the directory structure. The relative paths are relative to the current working directory denoted as a dot(.) while its parent directory is denoted with two dots(..).
OS module provides many such functions which can be used to work with files and directories. OS means Operating System.
Function used to find the current directory
getcwd( ) is a function which can be used to identify the current working directory.

For this : You have to import os module
For example,
>>> import os         # importing os module
>>> cwd=os.getcwd()       # getcwd function will return the path and store in cwd variable
>>> print(cwd)       # printing current path
OUTPUT
C:\Users\dell\AppData\Local\Programs\Python\Python37-32

STANDARD FILE STREAMS :
Standard file streams are realted to standard input and output device. Keyboard is a standard input device. Monitor/screen is a standard ouput device. Similarly, any error occurs is also displayed on a screen. So the monitor is also standard error device. That is,
Standard input device(stdin)    -           reads from keyboard
Standard output device(stdout)          -           prints on screen
Standard error device(stderr)  -           prints error on screen
In python. These devices are implemented as files known as standard streams.

To read data from the keyboard and to write data on a screen, we have to import module sys such as:
import sys
for reading, use function: sys.stdin.read()
for writing, use function: sys.stdout.write()
for writing error, use function : sys.stderr.write()
For example,
Reading and writing using stdin and stdout
import sys
f=open("book1.txt","r+")
ch=sys.stdin.read(3)         #whatever is read from the keyboard, return 3                         
                                              # characters from keyboard file and store in variable ch
print(ch)
sys.stdout.write(ch)
sys.stdin.read(5)
sys.stderr.write("\nNo errors occured\n")

import sys
f=open("book1.txt")
line1=f.readline()
line2=f.readline()
line3=f.readline()
sys.stdout.write(line1)
sys.stdout.write(line2)
sys.stdout.write(line3)
sys.stderr.write("\nNo errors occured\n")
f.close()

OUTPUT
welcome to python.          # written by keyboard
wel                                         # read 3 characters from stdin
No errors occurred                        # printing message
delhi is a capital of India.
delhi is a vast city
delhi has more than 1 crore population
No errors occured

No comments:

Post a Comment