File - Handling Part1
File - Handling Part1
Introduction to files
Files or Data Files are the Files that store data pertaining to a specific application, for later use.
The data files can be stored in various ways.
Types of files
o Text file
A text files stores information in ASCII or Unicode characters.
In text file every line is terminated or delimited with a special character called
EOL (End Of Line) character.
In Python the default EOL character is a new line character i.e (\n).
o Binary file
A Binary Fie is a file that contains information in machine format .
There is no delimeter for a line.
No translation occur as a result the processing is faster and easier for a
program to read and write as compared to text files
Binary format is the best and secure way to store an application data.
o CSV file
CSV stands for Comma Separated Values
Every record field values are separated with comma by default
It is a standard format of storing data
It can be used for data exchange in various applications
Relative and Absolute paths
o Path mentioned from from the top most level of hierarchy is known as Absolute
Path.
o Path mentioned relative to the current working directory is known as Relative Path.
"d:\\myfolder\\test\\STory.txt"
r"d:\myfolder\test\STory.txt"
"d:/myfolder/test/STory.txt"
"c://myfolder//test//story.txt"
r for read
w for write
a for append
r+ for read and write
w+ write and read
a+ append and read
fobj.close()
fileobj.seek(offset,reference point)
Reference point tells the position from where to move the file pointer . It
can be 0 (from beginning) , 1 (current position), 2 (end position).
pos=fileobj.tell()
flush() -> This function forcefully writes the content from buffer to
file .
f=open("Story.txt","w")
Example 2: Command to create a file Story.txt in d:\\myfolder\\test location and write content in it.
f=open("d:\\myfolder\\test\\STory.txt","w")
Examples:
'''
f=open("work.txt","a")
f.close()
'''
'''
f=open("Student.txt","w")
list1=[]
for i in range(5):
list1.append(name+'\n')
f.writelines(list1)
f.close()
'''
'''
f=open("work.txt","r")
data=f.read(10)
print(data)
f.close()
'''
'''
f=open("work.txt","r")
data=f.read()
print(data)
f.close()
'''
#read from file work.txt
'''
f=open("work.txt","r")
data1=f.read(100)
data2=f.read(8)
print(data1)
print(data2)
f.close()
'''
'''
f=open("work.txt","r")
pos=f.tell()
f.seek(10)
data=f.read(5)
pos=f.tell();
print("Data is ",data)
f.close();
'''
'''
f=open("work.txt","r+")
pos=f.tell()
f.seek(10)
f.write("ABCDEF")
f.close();
'''
f=open("work.txt","r")
data=f.read()
size=len(data)
f.close()
'''
'''
f=open("work.txt","r")
data=f.read()
cnt=0
for ch in data:
if ch=='w' or ch=='W':
cnt += 1
#print(data.count('w'))
f.close()
'''
'''
f=open("work.txt","r")
f.seek(10)
data=f.read(4)
print(data)
'''
'''
f=open("work.txt","r")
f.seek(10)
data=f.read(50)
print(data)
'''
'''
f=open("work.txt","r")
data=f.read()
data=data.lower()
cnt=0
for ch in data:
if ch in 'aeiou' :
cnt +=1
f.close()
'''
'''
f=open("work.txt","r")
data=f.read()
cnt=0
for ch in data:
if ch.isdigit() :
cnt +=1
f.close()
'''
#count number of words in a text file
'''
f=open("work.txt","r")
data=f.read()
wordlist=data.split()
print(wordlist)
f.close()
'''
'''
f=open("work.txt","r")
data=f.read()
cnt=0
wordlist=data.split()
if word[0]=='w' or word[0]=='W':
cnt+=1
f.close()
'''
'''
f=open("work.txt","r")
data=f.read()
cnt=0
wordlist=data.split()
if word=='we' or word=='We':
cnt+=1
'''
'''
f=open("work.txt")
data1=f.readline()
data2=f.readline()
print(data1,data2)
f.close()
'''
'''
f=open("work.txt")
for i in range(5):
data=f.readline()
print(data)
f.close()
'''
'''
f=open("work.txt")
all_lines=f.readlines()
print(all_lines)
print(len(all_lines))
print(all_lines[4])
f.close()
'''
#count how many lines begin with W.
'''
f=open('work.txt')
all_lines=f.readlines()
cnt=0
for l in all_lines:
if l[0]=='W':
cnt +=1
f.close()
'''
'''
f=open('work.txt','r')
all_lines=f.readlines()
cnt=0
for l in all_lines:
if l[-1]=='e':
cnt +=1
f.close()
'''
#remove all lines from 'work.txt' which starts with letter 'w'
f=open('work.txt')
all_lines=f.readlines()
cnt=0
for l in all_lines:
if l[0]=='W':
cnt +=1
f.close()
#Standard Input,Output,Error Streams
'''
stout-> monitor
stderr-> monitor
'''
import sys
line1=sys.stdin.readline()
print(line1)
‘’’’
import sys
line1=sys.stdin.readline()
sys.stdout.write(line1)