फ़ाइल में सामग्री जोड़ना

फ़ाइलों के साथ काम करने के लिए कई मोड उपलब्ध हैं:

मोड

कार्य

उदाहरण

'r'

पढ़ना (डिफ़ॉल्ट)

with open('document.txt', 'r') as f:

'w'

लिखना

with open('document.txt', 'w') as f:

'a'

जोड़ना

with open('document.txt', 'a') as f:

'r+'

पढ़ना + लिखना

with open('document.txt', 'r+') as f:

'x'

बनाना (यदि पहले से मौजूद है तो त्रुटि)

with open('document.txt', 'x') as f:

कंप्यूटर पर विभिन्न फ़ाइलें मौजूद हैं। आपका काम उनमें कुछ पंक्तियाँ जोड़ना है।

इनपुट में 2 लाइनें होती हैं। पहली लाइन फ़ाइल का नाम है। दूसरी लाइन वह सामग्री है जिसे हम उस फ़ाइल में जोड़ना चाहते हैं।

प्रोग्राम को पहले निर्दिष्ट पंक्ति को फ़ाइल में जोड़ना होगा और फिर फ़ाइल की पूरी सामग्री को प्रिंट करना होगा।

इनपुट

doc.txt (प्रारंभिक सामग्री)

आउटपुट

doc.txt (अंतिम सामग्री)

doc.txt
This is the second line

This is the first line

This is the first line
This is the second line

This is the first line
This is the second line

इनपुट

empty.txt (प्रारंभिक सामग्री)

आउटपुट

empty.txt (अंतिम सामग्री)

empty.txt
Adding some content

Adding some content

Adding some content

इनपुट

multiline.txt (प्रारंभिक सामग्री)

आउटपुट

multiline.txt (अंतिम सामग्री)

multiline.txt
The final line!

This is
some very
very long
document
with
many
lines of text…

This is
some very
very long
document
with
many
lines of text…
The final line!

This is
some very
very long
document
with
many
lines of text…
The final line!

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue