Python Create / Write File

📔 : Python 🔗

Python में किसी exist file को write करने के लिए दो modes use की जाती है -

  1. a
  2. w

a : अगर आपको file में बिना overwrite किये content add करना है तो file को open करते समय open('filename', 'a') function में "a" pass करना होगा। इससे जो भी file में content add करोगे वो file के existing content के end में append हो जायगा।

w : इसे file में old content remove होकर नया content write हो जायगा।

Python Write File

file write करने के लिए write(text) function का use किया जाता है , जिसमे argument के रूप में वो content pass किया जाता है जिसे हमें write करना है।


एक file test.txt है जो कि same directory में हैं - जिसमे कुछ इस तरह से content है

File : test.txt

Python :
Known as object oriented, server side language.
Developed By Guido Van Rossum in 1991
Mostly used in data analysis & AI.

अब इस file को new content append करने का एक program देखेंगे -

Python Write File Example
Copy Fullscreen Close Fullscreen
f = open('test.txt', "a")
# write file from new line.
f.write("\nPython is also used in web development , DJango is a famous frameWork for web development.")
f.close()

# now read file after appending new content..
fr = open('test.txt', "r")
print(fr.read())
Output
C:\Users\Rahulkumar\Desktop\python>python write_file.py
Python :
Known as object oriented, server side language.
Developed By Guido Van Rossum in 1991
Mostly used in data analysis & AI.
Python is also used in web development , DJango is a famous frameWork for web development.

तो कुछ इस तरह से Python में existing file में new content को append करते हैं , अब एक और program देखेंगे जिसमे file content को overwrite किया जयगा।

Python Overwrite File
Copy Fullscreen Close Fullscreen
# just pass w instead of a.
f = open('test.txt', "w")
f.write("Hello ! this is new overwritten content.")
f.close()
# now read file after overwriting.
fr = open('test.txt', "r")
print(fr.read())
Output
C:\Users\Rahulkumar\Desktop\python>python overwrite_file.py
Hello ! this is new overwritten content.

Python Create File

Python में file create करने के लिए open() function में नीचे दिए गए 3 modes में से कोई एक use कर सकते हैं -

w : इसका use use file overwrite करने के साथ - साथ new file create करने के लिए भी किया जाता है। pass किये गए filename की file अगर नहीं मिली तो दिए गए path पर उस name की new file create हो जायगी।

a : इसका use use file में new content append करने के अलावा new file create करने के लिए भी किया जाता है। अगर नहीं मिली तो दिए गए path पर उस pass किये गए name की new file create हो जायगी।

x : इसका use सिर्फ और सिर्फ नई file create करने के लिए ही किया जाता है , अगर किसी वजह से file create न हो पाई तो error generate होगी।

Python Cretae File Example

# It creates a new file : test2.txt
f = open('test2.txt', 'x')

Remember

x mode use करते समय अगर file पहले से exist है तो error generate होगी।

f = open('test.txt', 'x')

Traceback (most recent call last):
  File "create_file.py", line 1, in <module>
    f = open('test.txt', 'x')
FileExistsError: [Errno 17] File exists: 'test.txt'

इसके अलावा x, w, a mode में आप file को read नहीं कर सकते हैं। हाँ write जरूर कर सकते हैं।

#file mytest.text will create successfully.
f = open('mytest.txt', 'x')

#.when you will try to read it.
print(f.read())

Traceback (most recent call last):
  File "create_file.py", line 2, in <module>
    print(f.read())
io.UnsupportedOperation: not readable

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers