0% found this document useful (0 votes)
8 views11 pages

Python File Operations Guide

The document provides an overview of file operations in Python, including creating, opening, reading, writing, appending, and closing files. It also discusses using the os and sys modules for directory and system tasks, as well as handling text, numbers, and formatted files like CSV and TSV. Proper file management, including closing files, is emphasized throughout.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Python File Operations Guide

The document provides an overview of file operations in Python, including creating, opening, reading, writing, appending, and closing files. It also discusses using the os and sys modules for directory and system tasks, as well as handling text, numbers, and formatted files like CSV and TSV. Proper file management, including closing files, is emphasized throughout.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

File Operations in Python

Understanding Create, Open, Read,


Write, Append, and Close Files
Introduction to File Handling
• • File handling allows us to store data
permanently.
• • Python provides built-in functions to work
with files.
• • Common operations: Create, Open, Read,
Write, Append, Close.
Creating and Opening Files
• • Use open() function to create or open a file.
• Example:
• file = open('[Link]', 'w')
• print('File created successfully!')
• [Link]()
Reading Files
• • Use read(), readline(), or readlines() to read
data.
• Example:
• file = open('[Link]', 'r')
• data = [Link]()
• print(data)
• [Link]()
Writing and Appending Files
• • Write mode ('w') overwrites data.
• • Append mode ('a') adds data at the end.
• Example:
• file = open('[Link]', 'a')
• [Link]('Hello World!\n')
• [Link]()
Closing Files
• • Always close the file using close() to free up
resources.
• • Or use with-statement for automatic closing.
• Example:
• with open('[Link]', 'r') as file:
• data = [Link]()
• print(data)
Manipulating Directories using OS
Module
• • import os
• • [Link]('new_folder') → Create directory
• • [Link]() → Get current directory
• • [Link]() → List files
• • [Link]('[Link]') → Delete file
Using Sys Module
• • import sys
• • [Link] → Shows Python version
• • [Link] → Shows system path list
• • [Link]() → Exits program
Reading and Writing Numbers
• Example:
• with open('[Link]', 'w') as f:
• [Link](str(1234))

• with open('[Link]', 'r') as f:


• num = int([Link]())
• print(num)
Creating and Reading Formatted
Files (CSV, TSV)
• • CSV (Comma Separated Values) and TSV (Tab
Separated Values)
• Example (CSV):
• import csv
• with open('[Link]', 'w', newline='') as file:
• writer = [Link](file)
• [Link](['Name', 'Age'])
• [Link](['Divya', 18])
Summary
• • File operations: Create, Open, Read, Write,
Append, Close.
• • Use os and sys modules for file and system
tasks.
• • Handle text, numbers, and formatted files
(CSV, TSV).
• • Always close files properly.

You might also like