file operations

This commit is contained in:
2025-08-24 18:48:15 +05:30
parent 5f642d1c10
commit 26153becb1
6 changed files with 89 additions and 4 deletions

View File

@ -63,7 +63,6 @@ a = 10
b = 20
c = a
print(a is not b) # True
print(a is c) # True
# Ternary Operator in Python

View File

@ -1,5 +1,5 @@
# Its a special file that makes Python treat a directory as a package.
# it can be empty or we can exports with __all__
# it can be empty, or we can export with __all__
from .product_service import my_product_one
@ -10,7 +10,7 @@ __all__ = ["my_product_one"] # see below
# 1. from services import my_product_one
# 2. from services import helper_product_from_product_file # this will give error [cannot import name 'helper_product_from_product_file' from 'services']
# to use helper_product_from_product_file function or class we can able to access by package name (module name) like
# to use helper_product_from_product_file function or class we can access by package name (module name) like
# ==> from services.product_service import helper_product_from_product_file
# But we can able to access the my_product_one function like this
# But we can access the my_product_one function like this
# ==> from services import my_product_one

View File

@ -0,0 +1,84 @@
# import os
def read_file():
my_file = open("notes.txt", 'r')
print(my_file.read())
# good practices to close the file once completed our operations
my_file.close() # we need to close the file manually.to close automatically use 'with open'
# if the file is not exist it will create one and writes
# if the file exist it will clear all content then writes ours
my_second_file = open("notes2.txt", 'w')
my_second_file.write("This is my notes2 from code") # write function will add our data into buffer collection
# the close function will save the collected buffer data to file
my_second_file.close()
def read_text_file(file_path):
# if we use this it will automatically close the file after the code exclusion
with open(file_path, "r") as file:
print(file.read())
def read_line(file_path):
with open(file_path, "r") as file:
print(file.readline())
print(file.readlines())
# read() function will move the curser to the next line
# To move file's curser to top use seek 0 is first line
print(file.seek(0)) # seek(0) → go back to start of file
print(file.seek(10)) # seek(10) → move to the 10th character/byte
print(file.readline())
print(file.tell()) # file.tell() → gives the current cursor position (byte index).
def write_text_file(file_path, message):
with open(file_path, 'w') as file:
print(file.write(message))
def update_text_file(file_path, message):
with open(file_path, 'a') as file:
file.write("\n" + message)
if __name__ == "__main__":
read_file()
read_text_file('notes.txt')
read_line('notes.txt')
write_text_file(file_path="notes.txt", message="My sample file content")
update_text_file("notes.txt", "This line will update 2")
'''
Read modes
"r" → read text
"rb" → read binary
"r+" → read + write
Write modes
"w" → write text (overwrite/create)
"wb" → write binary (overwrite/create)
"w+" → read + write (overwrite/create)
Append modes
"a" → append text (create if not exists)
"ab" → append binary
"a+" → append + read
Exclusive creation modes
"x" → create new (fail if exists)
"xb" → create new binary (fail if exists)
"x+" → create new + read/write
'''

View File

@ -0,0 +1,2 @@
My sample file content
This line will update

Binary file not shown.