23 Prashant Python 22 A Exp11
23 Prashant Python 22 A Exp11
File Processing:
Reading a File:
• You can also use the function readline() to read one line at a
time
• You can also use the function readline() to read one line at a
time
f = open("file1.txt", "r")
str1 = f.readline()
print(str1)
str2 = f.readline()
print(str2)
print(f.readline())
Note that the string returned by readline() will always end with a
newline, hence, two newlines were produced after each sentence,
one by readline() and another by print()
Note that the string returned by readline() will always end with a
newline, hence, two newlines were produced after each sentence,
one by readline() and another by print()
f = open("file1.txt", "r")
str1 = f.readline()
print(str1, end = “”)
str2 = f.readline()
print(str2, end = “”)
print(f.readline(), end = “”)
f = open("file1.txt", "r")
for i in f:
print(i, end = "")
Writing to a File
• To write to a file, you can use the “w” or the “a” mode in
the open() function alongside the write() function as follows:
f = open("file1.txt", "w")
f.write("This file contains some information about
sales\n")
f.write("Total sales today = QAR100,000\n")
f.write("Sales from PCs = QAR70,000\n")
f.close()
Every time we run this code, the same above content will
be written (NOT
appended) to file1.txt since we are using the “w” mode
If, alternatively, we use the “a” mode, each time we run the
code, the same
above content will be appended to the end of file1.txt
• To write to a file, you can use the “w” or the “a” mode in
the open() function alongside the write() function as follows:
f = open("file1.txt", "w")
f.write("This file contains some information about
sales\n")
f.write("Total sales today = QAR100,000\n")
f.write("Sales from PCs = QAR70,000\n")
f.close()
• You can also write information into a text file via the
already familiar print() function as follows:
f = open("file1.txt", "w")
print("This file contains some information about sales",
file = f)
print("Total sales today = QAR100,000", file = f)
print("Sales from PCs = QAR70,000", file = f)
f.close()
import os
os.remove("file1.txt")
• You can also check if the file exists before you try to delete
it
import os
if os.path.exists("file1.txt"):
os.remove("file1.txt")
else:
print("The file does not exist")
Programs:
1. Copy file:
2. Rename file:
import os
cur_dir = os.getcwd()
print(cur_dir)
#os.mkdir("Temp2")
#os.makedirs("Temp1/temp1/temp2/")
#os.rmdir("Temp")
os.rename("Temp1","TEmp11")
3. Listing directory:
import os.path
os.listdir("c:\\")
print(os.listdir("c:\\"))
4. Print directory:
import os.path
def print_dir(dir_path):
for name in os.listdir(dir_path):
print(os.path.join(dir_path,name))
print_dir("c:\\users")
5. Create file:
import os
def create_another_file():
if os.path.isfile('test.txt'):
print('Trying to create a existing file')
else:
f=open('E:\test.txt',"w")
f.write(" This is how you create a new text file")
def reading_text():
file1=open("E:\\myfile1.txt","r")
text=file1.read()
print(text)
reading_text()
def reading_text_line_count():
file1=open("E:\\myfile1.txt","r")
text=file1.readlines()
for line in text:
print(len(line))
reading_text_line_count()
file1 = open("E:\\myfile.txt","r+")
file1.seek(0)
file1.seek(0)
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
7. Append operation:
def add_some_text():
file1=open("E:\\myfile1.txt","a")
l=["more text added"]
file1.writelines(l)
file1.write("Here is some additional text\n")
file1.close()
file1 = open("E:\\myfile1.txt","r+")
print("Output of Read function is ")
print(file1.read())
print()
add_some_text()
def add_more_text():
file1=open("E:\\myfile1.txt","a")
file1.write("""
Here is
some other
additional
text""")
file1.close()
file1 = open("E:\\myfile1.txt","r+")
print("Output of Read function is ")
print(file1.read())
print()
add_more_text()
8. Path:
import os.path
os.path.join("snakes","python")
#print(os.path)
print(os.path.split("C:\\Users\\Admin\\AppData\\Local\\Prog
rams\\Python"))
import os.path
def split_fully(path):
parent_path, name= os.path.split(path)
if name =="":
return(parent_path,)
else:
return split_fully(parent_path)+(name,)
print(split_fully("C:\\Users\\Admin\\AppData\\Local\\Progra
ms\\Python"))
import os.path
filename="E:\PYTHON\FP\my_file.txt"
if (os.path.isfile("E:\PYTHON\FP\my_file.txt")):
print("File Exists!!")
else:
print("File Doesnt Exists!!")
2. rename file
3.
4.
5.
6.
7.
8.
Conclusion: The ‘os’ module in Python empowers developers to interact
with the operating system, facilitating tasks such as file
operations, process management, and environment variable
handling within their programs.
References: Elearn.dbit
Don Bosco Institute of Technology Department of Computer Engineering
Performance Date :
Submission Date :
Batch :
Name of Student :
Roll No. :