File Handling in Python In Hindi

Python

File Handling in Python In Hindi – Python Me File Handling Bahut Important Part Hota Hai And Python Me File Handling Me Creating file, Opening fileReading fileWriting FileClosing file or Deleting file Jaise Kaam Kar Sakte Hai |

Yadi Aapne  HTML Full Course  And CSS Full Course Nhi Read Kiya Hai To Aap Vah Bhi Read Kar Sakte Hai |

Ham Is File Handling Chapter Mai Is text file, “masterprogramig.txt“, Ka Istemal Karenge |

File Handling in Python In Hindi

kisi Bhi Web Application Ka File Handling Ek Important Part Hota Hai And Isme Keval Aapko File Ko Open And Process Karne Ke Bad Aap Is File Handling Se Different Tasks Kar Sakte Hain |

Python Me File Ke Sath Har Ek Kaam Ke Liye Alag-Alag File Ke Liye Functions Bananye Gaye Hain |

  • open() : Opening File
  • write() : Writing or Creating File
  • read() , readline(): Reading File
  • close() : Closing File
  • remove() : Deleting File

Read More – Python Constructor and Destructor

close()  : Is Functions Ka Istemal Keval File ko Close Karne Ke Liye Hota Hain |

Python File Modes in Hindi

ModesDescription
rfile Ko read Kiya Jata Hai |
r+file Ko read or write Kiya Jata Hai|
wfile par data write Kiya Jata Hai| Agar file already Nhi Hoti To Use Create Kiya Jata Hai | Yaha Par file Mai Pahle store Hua data clear Kiya Jata Hai |
w+file Ko read Or write Kiya Jata Hai | Lekin file already Nhi Hoti To Use create Kiya Jata Hai | Yaha Par file Mai Pahle store Hua data clear Kiya Jata Hai |
afile Ko write Kiya Jata Hai | Agar file Mai already data Hota Hai To Diye Hue data Ko add Kiya Jata Hai| Agar file already Nhi Hoti To Use create Kiya Jata Hai |
a+file Ko read Or write Kiya Jata Hai | Agar file Mai already data Hota Hai To Diye Hue data Ko add Kiya Jata Hai| Agar file already Nhi Hoti To Use create Kiya Jata Hai |
xfile ko write Kiya Jata Hai | Agar file already Waha Par Hoti Hai To ‘File exist’ Ka error Aa Jata Hai |
x+file Ko read Or write Kiya Jata Hai | Agar file already Waha Par Hoti Hai To ‘File exist’ Ka error Aa Jata Hai |

Python open Functions in Hindi

open Function ko Keval Python Mai File Ko Open Karne Ke Liye Istemal Kiya Jata Hain And open Functions Mai 2 parameter pass Karte Hai |

Syntax:

open("file_name", "mode")
  • “file_name” : Jis file Ko open Karna Hai Us File Ka Name Yaha Par Input Kiya Jata Hai |
  • mode : Jis mode Par Is file Ko open Karna Hai Wo mode Yaha Par Aata Hai |

Example 1:

f = open("masterprograming.txt")
print(f)

Output:

<_io.TextIOWrapper name='masterprograming.txt' mode='r' encoding='cp1252'>

Example 2:

open("masterprograming.txt", "rt")

Yaha Hamne r – for read and t – for text ke liye Hai, Joki Ye Defaults Values Hoti Hai |

Python read Function In Hindi

read() Function in Python Ka Istemal Keval File se Data Ko Read Karne Ke Liye Kiya Jata Hain |

Syntax:

read(file, length_in_bytes)
  • file: Jis file Ko read Karna Hai Us file Ko open Kiya Jata Hai |
  • length_in_bytes: Jinte bytes read Karne Hai Unki Number Aate Hai |

Read More – Exception

Example 1:

f = open("masterprograming.txt", "r")
print(f.read())
f.close()

Output:

Hello! Welcome to masterprogramig.txt
This file is for testing purposes.
Good Luck!

Example 2:

f = open("masterprograming.txt", "r")
print(f.read(10))
f.close()

Output:

Hello! Wel

Example 3:

f = open("masterprograming.txt", "r")
print(f.readline())
f.close()

Output:

Hello! Welcome to masterprogramig.txt

Example 4:

f = open("masterprograming.txt", "r")
print(f.readline())
print(f.readline())
f.close()

Output:

Hello! Welcome to masterprogramig.txt

This file is for testing purposes.

Example 5:

f = open("masterprograming.txt", "r")
for x in f:
    print(x)
f.close()

Output:

Hello! Welcome to masterprogramig.txt

This file is for testing purposes.

Good Luck!

Python write Function In Hindi

write() Function In Python Se file par data write Kiya Jata Hai| Agar file already Nhi Hoti To Use Create Kiya Jata Hai |

Syntax:

write(file, "string", length_in_bytes)

Parameters :

  • file : Jis file Par write Karna Hai Us File Ko Open Kiya Jata Hai|
  • “string”: Jisko write Karna Hai Yaha Par Wo String Diya Jata Hai|
  • length_in_bytes: Ye optional Rahta Hai |Yaha Par bytes ke Number And Ye Bytes ki length return Karta hai |

Read More – Python OOPS Concepts

Example 1:

f = open("masterprograming.txt", "w")
f.write("Hello World")
f.close()

f = open("masterprograming.txt", "r")
print(f.read())
f.close()

Mode w Ka Istemal Karne Par File Ka Pahla Content Remove Ho Jata Hai And Jo New Content Write Hota Hai, Wo Save Ho Jata Hai |

Output:

Hello World

Example 2:

f = open("masterprograming.txt", "a")
f.write(" End Me Add Ho Gaya Hai")
f.close()

f = open("masterprograming.txt", "r")
print(f.read())
f.close()

Mode a Ka Istemal Karne Par File Ka Pahla Content Ke End Me Ye Content Add Ho Jata Hai, Isliye Jyadatar Mode a Ka Istemal Karte Hai |

Output:

Hello World End Me Add Ho Gaya Hai

Example 3:

f = open("myfile.txt", "x")
f.write("This is New File")
f.close()

f = open("myfile.txt",'r')
print(f.read())
f.close()

Mode x Ka Istemal , Agar File Create Nhi Pahle To Use Create Karna And Agar File Pahle Hi Create To Error Show Karne Ke Liye Mode x Ka Istemal Kiya Jata Hai |

Output:

This is New File

Python File Delete Function In Hindi

File Ko Delete Karne Ke Liye Python Mai remove() Function Ka Istemal Kiya Jata Hain |

Example 1:

import os
os.remove('myfile.txt')

Example 2:

import os
if os.path.exists('masterprograming.txt'):
    os.remove('masterprograming.txt')
else:
    print("File Does Not Exists")

Agar Aapko Folder Delete Karna Hai To Aap Niche Diye Code Ka Istemal Kare |

Example 3:

import os
os.rmdir('myfolder')

Read More – Python A to Z full Course In Hindi

Leave a Reply

Your email address will not be published. Required fields are marked *