[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Pyexp 7

This document outlines the implementation of file and directory handling in Python, detailing how to read, write, and manipulate files using built-in functions. It covers different file types, standard input/output/error streams, and provides example code for various file operations including opening, writing, reading, and deleting files. Additionally, it demonstrates file management tasks such as renaming and checking file existence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Pyexp 7

This document outlines the implementation of file and directory handling in Python, detailing how to read, write, and manipulate files using built-in functions. It covers different file types, standard input/output/error streams, and provides example code for various file operations including opening, writing, reading, and deleting files. Additionally, it demonstrates file management tasks such as renaming and checking file existence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Department of Computer Engineering

Experiment No.7
Aim: Write a program to implement File and directories

INTRODUCTION

File handling in Python allows users to read, write, and manipulate files stored on a system. It provides
built-in functions to handle text and binary files efficiently. Python treats files as streams:

 Text files: Store data in human-readable format (e.g., .txt, .csv).


 Binary files: Store data in machine-readable format (e.g., .jpg, .exe).

DATA FILES
Data files store structured information, which can be read and processed using Python. Python
supports different file formats like text files (.txt), CSV files (.csv), JSON files (.json), etc.

Syntax:
file = open("filename.txt", "mode")
# File operations
file.close()
Example:
file = open("data.txt", "w") # Creates a text file
file.write("Hello, Python File Handling!")
file.close()

OPENING AND CLOSING FILES


Before reading or writing to a file, it must be opened using Python’s open() function. After performing
operations, it should be closed using close() to free system resources.

Syntax:
file = open("filename.txt", "mode") # Open file
file.close() # Close file
Example:
file = open("sample.txt", "w")
file.write("This is a sample file.")
file.close()

READING AND WRITING FILES

Python provides methods like read(), readline(), and readlines() to read files and write() or writelines() to write
data.

Syntax:
# Writing to a file
file = open("filename.txt", "w")
file.write("Content")
file.close()

# Reading from a file


file = open("filename.txt", "r")
content = file.read()
print(content)
file.close()
Example:
# Writing data
file = open("example.txt", "w")
file.write("Python file handling example.")
file.close()
# Reading data
file = open("example.txt", "r")
print(file.read())
file.close()
STANDARD INPUT, OUTPUT, AND ERROR STREAMS

Python provides three standard streams:

1. Standard Input (sys.stdin) – Takes input from the user.


2. Standard Output (sys.stdout) – Displays output on the console.
3. Standard Error (sys.stderr) – Handles error messages.

Syntax:
import sys
# Standard input
data = sys.stdin.read()
# Standard output
sys.stdout.write("Output message\n")
# Standard error
sys.stderr.write("Error message\n")
Example:
import sys
sys.stdout.write("This is standard output.\n")
sys.stderr.write("This is an error message.\n")
Program:
import os
# 1. Opening a file in 'w+' mode (write and read)
file = open('yashodeep.txt', 'w+')
# 2. Writing to the file
file.write('AI is the future.\n')
file.write('It is transforming industries.\n')
# 3. Writing multiple lines
file.writelines(['Automation is key.\n', 'Ethical AI matters.\n'])
# 4. Moving cursor to the start and reading content
file.seek(0)
print('--- Full Content ---')
print(file.read())
# 5. Reset cursor and read line by line
file.seek(0)
print('--- Line by Line ---')
for line in file:
print(line.strip())
# 6. Reset cursor and read all lines into a list
file.seek(0)
print('--- List of Lines ---')
print([line.strip() for line in file.readlines()])
# 7. Closing the file
file.close()
# 8. Appending new content
with open('yashodeep.txt', 'a') as file:
file.write('AI will evolve further.\n')
# 9. Reading updated content
with open('yashodeep.txt', 'r') as file:
print('--- Updated Content ---')
print(file.read())
# 10. Checking file existence
if os.path.exists('yashodeep.txt'):
print('File exists.')
# 11. Renaming the file
os.rename('yashodeep.txt', 'ai_data.txt')
print('File renamed to ai_data.txt')
# 12. Deleting the file
os.remove('ai_data.txt')
print('File ai_data.txt deleted.')

Output:

You might also like