Saturday, 11 July 2026

πŸš€ Day 85/150 – File Write Operation in Python

 

πŸš€ Day 85/150 – File Write Operation in Python

Writing data to files is an essential skill in Python. Whether you're saving user input, creating reports, or logging application data, Python makes file writing simple and efficient. In this post, we'll explore four common methods to write data to a file.


Method 1 – Using write()

The write() method writes a string to a file. If the file doesn't exist, Python creates it automatically.

file = open("sample.txt", "w") file.write("Hello, World!") file.close()






Output (
sample.txt)
Hello, World!

Explanation:

  • "w" opens the file in write mode.
  • If the file already exists, its previous content is overwritten.
  • Always close the file after writing.

Method 2 – Using the with Statement

The with statement automatically closes the file after writing, making it the recommended approach.

with open("sample.txt", "w") as file: file.write("Welcome to Python!")



Output (sample.txt)
Welcome to Python!

Explanation:

  • No need to call close().
  • Safer and cleaner than manually opening and closing files.

Method 3 – Writing Multiple Lines with writelines()

Use writelines() to write multiple strings to a file.

lines = [ "Python\n", "Java\n", "C++\n" ] with open("sample.txt", "w") as file: file.writelines(lines)






Output (sample.txt)

Python
Java
C++

Explanation:

  • writelines() writes each string in the list.
  • Include \n if you want each item on a new line.

Method 4 – Taking User Input

Write user-provided text into a file.

text = input("Enter text: ") with open("sample.txt", "w") as file: file.write(text) print("Data written successfully!")





Sample Input

Learning Python is fun!

Output (sample.txt)

Learning Python is fun!

Explanation:
  • Accepts text from the user.
  • Saves it directly into the file.
  • Useful for forms, notes, and basic data storage.

Comparison of Methods

MethodBest For
write()Writing a single string
with open()Safe and recommended file handling
writelines()Writing multiple lines
User InputSaving user-generated content

πŸ”₯ Key Takeaways

  • Use write() to write a single string to a file.
  • Prefer with open() because it automatically closes the file.
  • Use writelines() when writing multiple lines at once.
  • Opening a file in "w" mode overwrites any existing content.
  • File writing is widely used for logging, reports, data storage, and automation.

Stay tuned for Day 86 of the #150DaysOfPython series! πŸš€

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (305) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) Books (276) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (39) Data Analytics (27) data management (16) Data Science (390) Data Strucures (23) Deep Learning (193) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (76) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (345) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (15) PHP (20) Projects (34) Python (1403) Python Coding Challenge (1187) Python Mathematics (4) Python Mistakes (51) Python Quiz (567) Python Tips (23) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)