Important Notice:

Course Content
File Processing in Python (Chapter-11) M3-R5
About Lesson

1. Opening File

             File के data को जब manipulate करना हो तो पहले file को open किया जाता है Python में file को open करने के लिए python के in-built ‘open()’ method का इस्तेमाल किया जाता है।

Syntax:-          obj = open(‘file’,’mode’)     #opens a file in given mode

Parameters : open() method के लिए 8 parameters होते है लेकिन यहाँ पर सिर्फ 2 महत्वपूर्ण parameters दिए गए है

fileName : यहाँ पर file name के साथ उसका extension भी दिया जाता है अगर same location पर file हो तो उसे path देने की जरुरत नहीं होती है

mode : यहाँ पर file को open करने के लिए mode दिया जाता है अगर mode दिया नहीं जाता है तो default ‘r'(reading) mode होता है

    f=open(“file_name.extension_name”, “mode”)

Eg.    f=open(“ak.txt”, “w”)

2. Closing File

            जब file को open() method से open किया जाता है तब file पर कुछ operations करके close भी किया जाता है File को close करने के लिए ‘close()’ method का इस्तेमाल किया जाता है।

   Syntax :             fileObj.close()                        or                 f.close() 

f=open(“abc.txt”,”w”)

f.write(“ak it solutionn”)

f.write(“YouTube Channeln”)

f.close()

फाइल को open करने के लिए opening mode बाताना पड़ता है

Text file को open करने के लिए कई mode है जो निम्न हैं-

i. w            ii.  r                  iii. a                 iv. w+              v. r+                vi. a+

 

 

 

1. Opening File

      When the data of a file has to be manipulated, the file is first opened. To open the file in Python, Python’s in-built ‘open()’ method is used.

Syntax:-    obj = open(‘file’,’mode’)     #opens a file in given mode

Parameters : There are 8 parameters for open() method but only 2 important parameters are given here.

fileName : Here, along with the file name, its extension is also given. If the file is at the same location, then there is no need to give the path.

mode : Here mode is given to open the file, if mode is not given then default is ‘r'(reading) mode.

           f=open(“file_name.extension_name”, “mode”)

Eg.   f=open(“ak.txt”, “w”)

2. Closing File

            When the file is opened with open() method, then it is also closed by performing some operations on the file. ‘Close()’ method is used to close the file.

       Syntax                        fileObj.close()             or         f.close()

f=open(“abc.txt”,”w”)

f.write(“ak it solutionn”)

f.write(“YouTube Channeln”)

f.close()

To open the file, opening mode has to be specified.

There are many modes to open a text file which are as follows-

i. w             ii. r                  iii. a                 iv. w+              v. r+                vi. a+

error: Content is protected !!