Skip to main content

Pyhton : File I/O (CWH)

 

# Open,read,write on text file

f=open("Sample text","r")   #open the file in read mode
f=open("Sample text","w")   #open the file in write mode
f=open("Sample text","a")   #open the file in appending mode
f=open("Sample text","+")   #open the file in updating mode
f=open("Sample text","rb")  #open the file in binary mode
f=open("Sample text","rt")  #open the file in text mode

with open("sample.txt") as f:   #best way open and auto close file

text=f.read()   #read its contents
print(text)     #print its contents

f.close()       #close the file


# Program to read line by line from a text file


f = open('sample.txt')

# read first line
data = f.readline()
print (data)

# Read second line
data = f.readline()
print(data)

# Read Third line
data = f.readline()
print(data)

f.close()


# Program to read the text from a given file anf find out wheater it
  contains the word or not

# f=open("file.txt")        #not working(vikas use pycharm)
f=open(r"G:\My Drive\Coding\Python\Start\file.txt")
t=f.read()
w=input("Enter Word to find in text tile : ")

if w in t:
    print("Word Found")
else:
    print("Not Found")
f.close()


# Program to





Popular posts from this blog

C++ : Calculator

  # include < iostream > using namespace std ; int main () {     int a , b ;     char o ;     float c ;     cout << " Enter Value of A \n " ;     cin >> a ;     cout << " Enter Value of B \n " ;     cin >> b ;     cout << " Enter Operation \n " << " + for Addition \n - for substration \n * for multiplication \n / for division \n " ;     cin >> o ;     switch ( o )     {         case ' + ' :       {            c = a + b ;             break ;       }       case ' - ' :       {           c = a - b ;           break ;       }       case ' / ' :       {         ...

C++ : Pointer Program

# include < iostream > using namespace std ; int main () {     int a = 4 ;     int * b =& a ;     // & is address of operator     cout << " Thr address of a is " << & a << endl ;     cout << " Thr address of a is " << b << endl ;     // * is value at dereference operator     cout << " Thr value of a is " << * b << endl ;           // Pointer to pointer     int ** c =& b ; cout << " The adress of b is " << & b << endl ;     cout << " The adress of b is " << c << endl ;     cout << " The value at address c is " << * c << endl ;     cout << " The value at address value_at (value_at( c )) is " << ** c << endl ;     } OUTPUT =>           Thr address of...

Python : Small Projects (CWH)

  # Project 1 : Snake, Water, Game OR Rock,Paper,Scissor import random def game ( comp , you ) :     if comp == you :         return 2     elif comp == " s " :         if you == " w " :             return 0         elif you == " g " :             return 1     elif comp == " w " :         if you == " g " :             return 0         elif you == " s " :             return 1     elif comp == " g " :         if you == " w " :             return 0         elif you == " s " :             return 1 print ( " Computer turn : Snake(s),Water(w) or Gun(g) " ) randno = random . randint ( 1 , 3 ) if randno == 1 :  ...