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

Friday, 3 April 2020

ASSIGNMENT ON SQL QUERIES





SUBJECT: INFORMATICS PRACTICES
ASSIGNMENT-1
TOPIC: SQL QUERIES

Q1. Consider the table Hospital given below and write commands in SQL for (i) to (xii)
 Hospital
No
Name
Age
DEPARTMENT
DateOfAdm
Charges
Sex
1
Sandeep
64
Surgery
23/02/98
300
M
2
Ravina
24
Orthopedic
20/01/98
200
F
3
Karan
45
Orthopedic
10/02/98
200
M
4
Tarun
12
Surgery
01/01/98
300
F
5
Zubin
36
ENT
12/01/98
250
M
6
Ketaki
16
ENT
12/02/98
300
F
7
Ankita
29
Cardiology
20/02/98
800
F
8
Zareen
45
Gynecology
22/02/98
Null
F
9
Kush
19
Cardiology
13/01/98
800
M
10
Shailya
31
Medicine
19/02/97
400
F

(i)    To show all information about the patients of cardiology department.
(ii)    To list the names of female patients who are in orthopaedic department.
(iii)   To display Patient’s name, charges, Age for  male and female patients.
(iv)  To count the number of patients with Age > 30.
(v)    Increase the charges of male patient in ENT department by 3%.
  (vi) Add another column email_id with suitable data type.
  (vii) Delete the records of all female patients in Surgery department.
  (viii)Display a report listing name, age, charges and amount of charges including VAT as 2%  on charges name the column as total charges and keep the data in ascending order of name.
  (ix)To display the difference of highest and lowest charges of each department having maximum charges more than 300.
  (x) Find out the details of patients whose age  is same or more than that of patient whose hospital charges are maximum.
 (xi)Display the details of all the patients who are hospitalised in 1998.
 (xii)Display the charges of various departments .A charge amount should appear only once.  
Find out the  output for SQL commands (xiii) to (xvi).
  (xiii)SELECT COUNT(DISTINCT  Department) FROM HOSPITAL ;
  (xiv)SELECT MAX(Age) FROM HOSPITAL  WHERE SEX=’M’;
  (xv)SELECT AVG(Charges) FROM HOSPITAL  WHERE SEX=’F’;
  (xvi)SELECT SUM(Charges) FROM HOSPITAL  WHERE DATEOFadm < ’12/08/98’ ;
Q2.
(a)
Write an SQL command for creating a table student whose structure is given below:
FIELD NAME
DATATYPE
SIZE
CONSTRAINT
Rno
Number
3
Part of Primary Key
Class
Varchar
5
Part of Primary Key
Percentage
Number
5,2
>0 and <=100
Projno
Number
6
FK –Project(pno)
Address
Varchar
30
Default Hyderabad
            


(b) The Title and Price columns of table “Library” are given below:
TITLE                      PRICE
Mastering C++     295
Guide Network   300
Mastering SQL     450
Dos GUIDE            400
Basic for beginners  299
Mastering Window  Null
Based on this information ,find the output of the following queries:
(a)    SELECT MIN(Price)from library;
(b)   SELECT COUNT(Title) from library WHERE Price < 150;
(c)    Select  AVG(price) from library WHERE title like ‘%e%’;
(d)   Select title from library where price = (select max(price) from library);  
                                                                                         

(c)    A table ACCOUNT in a database has 3 columns and 30 rows. The DBA has added 3 more columns and 50 more rows to the table. But the table has about 15 records where balance is null. What is the degree and cardinality of this table now ?


ASSIGNMENT ON FILE HANDLING


             

SUBJECT: COMPUTER SCIENCE

ASSIGNMENT-1
TOPIC: FILE HANDLING

    1.     A text file named computer.txt contains some text which needs to be displayed such that every next character is separated by a symbol #. Write a program to do this.
    2.     WAP in python to open a text file story.txt so that new contents can be added at the end of file in it.
    3.     WAP to read lines from the text file india.txt and to find and display the occurrence of the word “India”.
If the content of the file is:
India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market
Most of the Indians can foresee the heights that India is capable of reaching.
The output should be : 4

   4.     WAP to read the file story.txt and count the number of lines that begins with ‘A’ or ‘E’.
   5.     WAP to read the file book.txt and copy all its contents to author.txt.
   6.     WAP to read the file book.txt and count total number of alphabets and digits.
   7.     WAP to find the longest word in the file story.txt.
   8.     WAP to count total no. of spaces, words, characters and lines in the file message.txt.
   9.      Differentiate between file modes r+ and rb+ with respect o python.
   10.                         Differentiate between text file and binary file.