SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
File and Directories in Python
February 14, 2018
1 Files
• Location within a disk to store data / information in computer memory
• Files are accesed using a ’path’ and ’unique name’.
• In Python we can directly use file operation functions
• File manipulations are done using file object
• While working with files follow the sequence
1. Open a file
2. Perform operation (Read / Write / Append)
3. Close the file
1.1 Opening and Closing Files
1.1.1 open()
• Python provides a built-in function open(), to open a file, which returns a file object.
• Syntax:
– file_object = open(file_name, [access_mode],[buffering])
* access_mode : [r | w | r+ | w+ | a | a+ | x]
* buffering
· 0 : no buffering
· 1 : enables buffering during file access.
• The second argument to open is optional, which defaults to ’r’ when not specified.
• Unix does not distinguish binary files from text files but windows does.
• On windows ’rb’, ’wb’, ’ab’ should be used to open a binary file in read, write and append
mode respectively.
• File Attributes
– file.closed
– file.name
– file.mode
1.1.2 close()
• File object is closed after flushing th unwritten information with the help of close()
1
• Python automatically closes a file when the file’s reference object is reassigned to some other
file.
• Recommended to close the file explicitly.
In [16]: f = open("test.txt","w+")
print("File name :", f.name)
print("File mode :", f.mode)
print("File closed: ", f.closed)
f.close()
print("File closed: ", f.closed)
File name : test.txt
File mode : w+
File closed: False
File closed: True
1.2 File Attributes
1. file.closed
• If file is closed, it returns "True", else it returns "False"
2. file.name
• It returns the file name
3. file.mode
• It returns the mode in which the file was opened
In [3]: myfile = open("test.txt","rb+")
print("File Name : ", myfile.name)
print("File mode : ", myfile.mode)
print("File is closed or not :", myfile.closed)
myfile.close()
print("File is closed or not :", myfile.closed)
File Name : test.txt
File mode : rb+
File is closed or not : False
File is closed or not : True
1.3 Reading and writing files
1.3.1 write()
• for writing data into an open file
• Syntax :
– file_object.write(string)
2
1.3.2 read()
• A string can be read from an open file with the help of read()
• Syntax:
– file_object.read([count])
* count : how many bytes are to be read from an open file
• Reads the file from the beginning, if count is not provided
1.3.3 readline() and readlines()
• Contents of a file can be read line-wise using readline() and readlines()
• readline()
– returns empty string when there is nothing more to read in a file.
In [4]: # Writing into a file using write()
file = open("test.txt","w+")
file.write("Hello friends!! nLets do Python Programming!!")
file.close()
print('read file')
file = open("test.txt",'a+')
file.write("nBegin coding")
file.close()
read file
In [5]: # Reading from a file using read()
file = open("test.txt","r")
str1 = file.read()
print(str1)
file.close()
Hello friends!!
Lets do Python Programming!!
Begin coding
In [6]: # Reading from a file using readline()
f = open('test.txt')
str1 = f.readline()
while str1 != '':
print(str1.strip('n'))
str1 = f.readline()
f.close()
Hello friends!!
Lets do Python Programming!!
Begin coding
3
In [7]: # Reading from a file using readlines()
list1 = open('test.txt').readlines()
for l in list1:
print(l.strip('n'))
print(list1)
Hello friends!!
Lets do Python Programming!!
Begin coding
['Hello friends!! n', 'Lets do Python Programming!!n', 'Begin coding']
In [8]: # writelines()- is convenient to use when the data is available as a list of lines.
f = open('test2.txt','w+')
f.writelines(['an', 'bn', 'cn'])
f.close()
f = open('test.txt','r')
for l in f.readlines():
print(l.strip('n'))
f.close()
Hello friends!!
Lets do Python Programming!!
Begin coding
1.4 File Positions
• In Python, we can check and change the current positions of the file object with the help of
the following functions
1. tell()
– to know the current position in the file
– it returns the current position in the form of an integer.
2. seek()
– used to change the current position of the file
– syntax: seek(offset [,from])
* offset : specifies the number of bytes to be moved
* from : indicates the reference position, starting from where the bytes are to be
moved.
· 0, if reference position == file beginning
· 1, if reference position == current file position
· 2, if reference position == file end
In [9]: myfile = open("test.txt","r+")
str = myfile.read(10)
print(str)
pos = myfile.tell()
print("Current File position = ",pos)
4
Hello frie
Current File position = 10
In [10]: str = myfile.read(20)
print(str)
pos = myfile.tell()
print("Current File position = ",pos)
myfile.seek(0,0)
pos = myfile.tell()
print("Current File position = ",pos)
myfile.close()
nds!!
Lets do Pytho
Current File position = 30
Current File position = 0
1.5 Rename and Remove Files
• Done using the os module
– rename()
* used to change the name of the given file.
* it takes two arguments,
· current file name
· new file name
– remove()
* used to delete files
* takes one argument
· file name
In [11]: import os
os.rename("test.txt","trial.txt")
try:
os.remove("test.txt")
except FileNotFoundError:
print("File does't exists")
File does't exists
2 Directories
• Files are collectively placed within different directories
• Diectories can be organized using the built-in functions in os module.
5
– mkdir("namedir")
* to make a directory inside the current directory
– getcwd()
* to return the complete path of the current working directory
– chdir("name dir")
* to change the current working directory
– rmdir("name")
* to delete any directory
In [30]: import os
# help(os)
c = os.getcwd()
print("Current Working Directory : ", c)
Current Working Directory : /home/lifna/Desktop/OST_Lab/Notebooks
In [32]: os.mkdir("newdir")
l = os.listdir()
if "newdir" in l:
print("Created new directory")
else:
print("Directory not present")
print()
print("After deleting Directory")
os.rmdir("newdir")
l = os.listdir()
if "newdir" in l:
print("Created new directory")
else:
print("Directory not present")
Created new directory
After deleting Directory
Directory not present
3 Extra Programs
3.1 Write a program to list all files in the given directory.
In [4]: import os
p = "//home//lifna//Desktop//OST_Lab"
l = os.listdir(p)
for i in l:
print(i)
6
Perl pgms
exception.py~
Python ppts
.ipynb_checkpoints
Perl ppts
Python pgms
Notebooks
.Rhistory
raise.py~
3.2 Write a program extcount.py to count number of files for each extension in the
given directory.
• Input : directory name as argument
• Execute : python extcount.py src/
• Output : print count and extension for each available file extension.
14 py
4 txt
1 csv
In [67]: def ext_frequency(files):
ext_files ={}
for w in files:
ext_files[w] = ext_files.get(w,0) + 1
return ext_files
def main():
l = os.listdir()
new_l = []
for i in range(0,len(l)-1):
y =l[i].split('.')
if y[1] != None:
new_l.append(y[1])
frequency = ext_frequency(new_l)
for ext, count in frequency.items():
print(count,ext)
if __name__ =="__main__":
main()
6 ipynb
2 pdf
1 ipynb_checkpoints
1 tex
7
3 png
4 txt
3.3 Write a program to list all the files in the given directory along with their length
and last modification time.
• Input : Directory name
• Output : should contain one line for each file
– filename length modification date
• Hint: os.stat.
In [44]: import os
p = "//home//lifna//Desktop//OST_Lab//Notebooks"
l = os.listdir(p)
print('{:10}{:15}{:>60}'.format('Size','Modi_time','Name'))
for i in l:
#print(os.stat(i)[6]," ", os.stat(i)[8]," ",i)
print('{:10}{:15}{:>60}'.format(os.stat(i)[6],os.stat(i)[8],i))
Size Modi_time Name
128030 1518583006 notebook.tex
70676 1517888227 raise.png
20503 1517908144 Exception Handling in Python.ipynb
37416 1518478223 Arrays in Python.ipynb
555 1518575856 Text Processing & Regular Expressions in Python.ipynb
152803 1518484088 Exception Handling in Python.pdf
28449 1518586881 File and Directories in Python.ipynb
6 1518580541 test2.txt
2380 1518478449 GUI Programming in Python.ipynb
65972 1517887221 finally.png
0 1518580725 test.txt
58061 1518483826 Arrays in Python.pdf
13169 1517977267 Lists in Python.ipynb
4096 1518575765 .ipynb_checkpoints
58 1518580536 trial.txt
45 1517892489 testfile
107336 1517886592 try_except.png
176 1518493459 she.txt
48 1517990309 text.txt
3.4 Write a program to print directory tree.
• The program should take path of a directory as argument and print all the files in it recur-
sively as a tree.
• Execute : python dirtree.py foo
8
• Hint : os.walk already does the top-down, depth-first walk
foo
|-- a.txt
|-- b.txt
|-- code
| |-- a.py
| |-- b.py
| |-- docs
| | |-- a.txt
| | -- b.txt
| -- x.py
-- z.txt
In [69]: import os
def list_files(startpath):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print('{}{}'.format(subindent, f))
path = "//home//lifna//Desktop//OST_Lab"
list_files(path)
OST_Lab/
exception.py~
.Rhistory
raise.py~
Perl pgms/
hello.pl
Python ppts/
Python Text Processing.pdf
Strings1.ppt
Python_Files_2.pdf
Database Python.pdf
introduction-1.pptx
6_Arrays_84_Python.pdf
Modules_Python.pdf
Python Packages.pdf
Python Files_1.pdf
Python_img_proc.pdf
Python Modules_2.pdf
Module_1/
2_1_2_Reading Writing Files.pdf
9
Module 1 - Python Basics (Part 5).pdf
raise.png
1_16_Advanced OOP.pdf
Module 1 - Python Basics (Part 2).pdf
1_3_1_Input_Output.pdf
Module 1 - Python Basics (Part 3).pdf
1_12_Customizing Classes and Overlaodong.pdf.crdownload
1_3_2_Input and Output.pdf
Array_tables.odt
1_8_Lists_2.pdf
1_2_Basic Operators.pdf
Module 1 - Python Basics (Part 4).pdf
1_7_Functions.pdf
1_10_Exception Handling_Final.pdf
1_8_Tuples.pdf
1_10_Exception Handling.pdf
1_8_Lists.pdf
1_4_2_Control Statements.pdf
1_0_Introduction to Python.pdf
2_5_6_ Text Processing and REgular Expressions.pdf
1_11_Introduction to OOP.pdf
1_4_1_Control Flow Statements.pdf
1_6_Strings and Characters.pdf
1_9_List_tuples_Dictionaries.pdf
1_12_13_Classes and Objects.pdf
2_1_File Processing.pdf
4_1_GUI.pdf
1_14_Abstract Classes and Interfaces.pdf
1_15_Inheriatance and Polymorphism.pdf
beginners_python_cheat_sheet_pcc_all.pdf
Module 1 - Python Basics (Part 1).pdf
1_1_DataTypes.pdf
.ipynb_checkpoints/
GUI Programming in Python-checkpoint.ipynb
File and Directories in Python-checkpoint.ipynb
Lists in Python-checkpoint.ipynb
Exception Handling in Python-checkpoint.ipynb
Arrays in Python-checkpoint.ipynb
Perl ppts/
Beginers Perl.pdf
Perl_Day2.pdf
Perl_Day1.pdf
IO_Files_Directories.pdf
Python pgms/
head_tail.py
TimeClass.pyc
TimeClass2.py~
lena.png
10
TimeClass_spAttributes.py~
TimeClass.py~
face.png
MyClass.pyc
exception.py~
TimeClass_spAttributes.py
TimeObject_spAttributes.py~
MyClass_protected.py~
raise.py
Time2.py~
Time.py~
Time.py
MyClass.py~
tail.py
exception.py
TimeObject_spAttributes.py
Time2.py
TimeClass2.pyc
TimeClass2.py
Time.pyc
TimeClass.py
chain_compare.py
MyClass_protected.py
chain_compare.py~
MyClass.py
Notebooks/
notebook.tex
raise.png
Exception Handling in Python.ipynb
Arrays in Python.ipynb
Text Processing & Regular Expressions in Python.ipynb
Exception Handling in Python.pdf
File and Directories in Python.ipynb
test2.txt
GUI Programming in Python.ipynb
finally.png
test.txt
Arrays in Python.pdf
Lists in Python.ipynb
trial.txt
try_except.png
she.txt
text.txt
.ipynb_checkpoints/
File and Directories in Python-checkpoint.ipynb
Text Processing & Regular Expressions in Python-checkpoint.ipynb
11
3.5 Word Count
• To compute the number of characters, words and lines in a file
In [12]: # Number of characters in a file is same as the length of its contents.
def charcount(filename):
return len(open(filename).read())
# Number of words in a file can be found by splitting the contents of the file.
def wordcount(filename):
return len(open(filename).read().split())
# Number of lines in a file can be found from readlines method.
def linecount(filename):
return len(open(filename).readlines())
file = input("Enter the file name :").strip()
char_count = charcount(file)
print("Number of characters :",char_count)
word_count = wordcount(file)
print("Number of words :",word_count)
line_count = linecount(file)
print("Number of lines :", line_count)
Enter the file name :trial.txt
Number of characters : 58
Number of words : 8
Number of lines : 3
3.6 Word Frequency
• To find number of occurrences of each word in a file.
• Dictionary can be used to store the number of occurrences for each word.
In [41]: # Lets first write a function to count frequency of words, given a list of words.
def word_frequency(words):
"""
Returns frequency of each word given a list of words.
word_frequency(['a', 'b', 'a'])
{'a': 2, 'b': 1}
"""
frequency = {}
for w in words:
frequency[w] = frequency.get(w,0) + 1
return frequency
12
# Getting words from a file is very trivial.
def read_words(filename):
return open(filename).read().split()
# We can combine these two functions to find frequency of all words in a file.
def main(filename):
frequency = word_frequency(read_words(filename))
for word, count in frequency.items():
print(word, count)
if __name__ =="__main__":
# import sys
# main(sys. argv[1])
fname = input("Enter the file name:")
main(fname)
Enter the file name:she.txt
seashore, 1
seashore 1
I'm 2
if 1
seashore; 1
shells. 1
she 2
are 2
seashells 3
on 2
sure 1
The 1
shells 2
She 1
the 3
So 1
sells 3
sure. 1
that 2
3.7 Write a program reverse.py to print lines of a file in reverse order.
• Input : she.txt
– She sells seashells on the seashore;
– The shells that she sells are seashells I’m sure.
– So if she sells seashells on the seashore,
– I’m sure that the shells are seashore shells.
• Execute : python reverse.py she.txt
• Output
13
– I’m sure that the shells are seashore shells.
– So if she sells seashells on the seashore,
– The shells that she sells are seashells I’m sure.
– She sells seashells on the seashore;
In [13]: list1 = open("she.txt",'r').readlines()
#print(list1)
l = len(list1)
list1.reverse()
for l in list1:
print(l,end='')
I'm sure that the shells are seashore shells.
So if she sells seashells on the seashore,
The shells that she sells are seashells I'm sure.
She sells seashells on the seashore;
3.8 Program to print each line of a file in reverse order.
In [19]: def reverse(st):
s = ""
for i in st:
s = i + s
return s
list1 = open("she.txt",'r').readlines()
#print(list1)
tlist = []
for l in list1:
l = l.strip('n')
#rev = reverse(l)
print(reverse(l))
;erohsaes eht no sllehsaes slles ehS
.erus m'I sllehsaes era slles ehs taht sllehs ehT
,erohsaes eht no sllehsaes slles ehs fi oS
.sllehs erohsaes era sllehs eht taht erus m'I
3.9 Implement unix commands head and tail.
• The head and tail commands take a file as argument and prints its first and last 10 lines of
the file respectively.
In [ ]: ## Save the program as head_tail.py
import sys
def tail(fname, num_lines):
14
with open(fname) as f:
content = f.read().splitlines()
count = len(content)
for i in range(count-num_lines,count):
print(content[i])
def head(fname, nlines):
with open(fname) as f:
content = f.read().splitlines()
count = len(content)
for i in range(0,nlines):
print(content[i])
if len(sys.argv) !=3:
print('Usage: tail.py <file> <nlines>')
sys.exit(1)
else:
fname, nlines = sys.argv[1:]
num_lines = int(nlines)
try:
f = open(fname,"r")
if f == None:
raise IOError
else:
print("First 10 lines in the file : ", fname)
print("#################################")
head(fname,num_lines)
print()
print()
print("Last 10 lines in the file : ", fname)
print("#################################")
tail(fname,num_lines)
except IOError:
print("File doesn't exists in current working directory")
3.10 Write a program center_align.py to center align all lines in the given file.
• Input : she.txt
– She sells seashells on the seashore;
– The shells that she sells are seashells I’m sure.
– So if she sells seashells on the seashore,
– I’m sure that the shells are seashore shells.
• Execute : python center_align.py she.txt
• Output :
– I’m sure that the shells are seashore shells.
15
– So if she sells seashells on the seashore,
– The shells that she sells are seashells I’m sure.
– She sells seashells on the seashore;
In [27]: list1 = open("she.txt",'r').readlines()
for l in list1:
l = l.strip('n')
print(l.center(60))
She sells seashells on the seashore;
The shells that she sells are seashells I'm sure.
So if she sells seashells on the seashore,
I'm sure that the shells are seashore shells.
3.11 Write a function extsort to sort a list of files based on extension.
• Input : extsort([’a.c’, ’a.py’, ’b.py’, ’bar.txt’, ’foo.txt’, ’x.c’])
• Output : [’a.c’, ’x.c’, ’a.py’, ’b.py’, ’bar.txt’, ’foo.txt’]
In [15]: def extsort(x):
i=0
while(i<len(x)):
x[i]=x[i].split('.')
i=i+1
x.sort(key=lambda x:x[1])
i=0
while(i<len(x)):
x[i]=".".join(x[i])
i=i+1
return x
print(extsort(['a.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt', 'x.c']))
['a.c', 'x.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt']
3.12 Implement unix command grep.
• Input : a string and a file as arguments
– String : she
– File : she.txt
* She sells seashells on the seashore;
* The shells that she sells are seashells I’m sure.
* So if she sells seashells on the seashore,
* I’m sure that the shells are seashore shells.
• Execute : python grep.py she.txt sure
• Output : prints all lines in the file which contain the specified string.
16
– The shells that she sells are seashells I’m sure.
– I’m sure that the shells are seashore shells.
3.13 Write a program wrap.py that takes filename and width as aruguments and wraps
the lines longer than width.
• Input : she.txt
– She sells seashells on the seashore;
– The shells that she sells are seashells I’m sure.
– So if she sells seashells on the seashore,
– I’m sure that the shells are seashore shells.
• Execute : python wrap.py she.txt 30
• Output :
– I’m sure that the shells are s
– eashore shells.
– So if she sells seashells on t
– he seashore,
– The shells that she sells are
– seashells I’m sure.
– She sells seashells on the sea
– shore;
3.14 write a new program wordwrap.py that works like wrap.py, but breaks the line
only at the word boundaries?
• The above wrap program is not so nice because it is breaking the line at middle of any word.
Can you
• Input : she.txt
– She sells seashells on the seashore;
– The shells that she sells are seashells I’m sure.
– So if she sells seashells on the seashore,
– I’m sure that the shells are seashore shells.
• Execute : python wordwrap.py she.txt 30
• Output :
– I’m sure that the shells are
– seashore shells.
– So if she sells seashells on
– the seashore,
– The shells that she sells are
– seashells I’m sure.
– She sells seashells on the
– seashore;
17

Contenu connexe

Tendances

Tendances (20)

Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Linked list
Linked listLinked list
Linked list
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Set data structure
Set data structure Set data structure
Set data structure
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
NumPy
NumPyNumPy
NumPy
 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
virtual function
virtual functionvirtual function
virtual function
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Stacks
StacksStacks
Stacks
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
List in Python
List in PythonList in Python
List in Python
 

Similaire à File and directories in python

File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleRohitKurdiya1
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxAshwini Raut
 
Python programming
Python programmingPython programming
Python programmingsaroja20
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptxVishuSaini22
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdfProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdflailoesakhan
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugevsol7206
 

Similaire à File and directories in python (20)

File Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai saleFile Handling Topic for tech management you know na tho kyuon puch raha hai sale
File Handling Topic for tech management you know na tho kyuon puch raha hai sale
 
files.pptx
files.pptxfiles.pptx
files.pptx
 
FIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptxFIle Handling and dictionaries.pptx
FIle Handling and dictionaries.pptx
 
Python programming
Python programmingPython programming
Python programming
 
Python File functions
Python File functionsPython File functions
Python File functions
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Files in c++
Files in c++Files in c++
Files in c++
 
Python IO
Python IOPython IO
Python IO
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File management
File managementFile management
File management
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Python - Lecture 8
Python - Lecture 8Python - Lecture 8
Python - Lecture 8
 
Python - Lecture 9
Python - Lecture 9Python - Lecture 9
Python - Lecture 9
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File mangement
File mangementFile mangement
File mangement
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdfProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
ProgFund_Lecture_6_Files_and_Exception_Handling-3.pdf
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 

Plus de Lifna C.S

Mobile Design
Mobile DesignMobile Design
Mobile DesignLifna C.S
 
Mobile Information Architecture
Mobile Information ArchitectureMobile Information Architecture
Mobile Information ArchitectureLifna C.S
 
Types of Mobile Applications
Types of Mobile ApplicationsTypes of Mobile Applications
Types of Mobile ApplicationsLifna C.S
 
Arrays in python
Arrays in pythonArrays in python
Arrays in pythonLifna C.S
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonLifna C.S
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in pythonLifna C.S
 

Plus de Lifna C.S (7)

Mobile 2.0
Mobile 2.0Mobile 2.0
Mobile 2.0
 
Mobile Design
Mobile DesignMobile Design
Mobile Design
 
Mobile Information Architecture
Mobile Information ArchitectureMobile Information Architecture
Mobile Information Architecture
 
Types of Mobile Applications
Types of Mobile ApplicationsTypes of Mobile Applications
Types of Mobile Applications
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 

Dernier

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 

Dernier (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 

File and directories in python

  • 1. File and Directories in Python February 14, 2018 1 Files • Location within a disk to store data / information in computer memory • Files are accesed using a ’path’ and ’unique name’. • In Python we can directly use file operation functions • File manipulations are done using file object • While working with files follow the sequence 1. Open a file 2. Perform operation (Read / Write / Append) 3. Close the file 1.1 Opening and Closing Files 1.1.1 open() • Python provides a built-in function open(), to open a file, which returns a file object. • Syntax: – file_object = open(file_name, [access_mode],[buffering]) * access_mode : [r | w | r+ | w+ | a | a+ | x] * buffering · 0 : no buffering · 1 : enables buffering during file access. • The second argument to open is optional, which defaults to ’r’ when not specified. • Unix does not distinguish binary files from text files but windows does. • On windows ’rb’, ’wb’, ’ab’ should be used to open a binary file in read, write and append mode respectively. • File Attributes – file.closed – file.name – file.mode 1.1.2 close() • File object is closed after flushing th unwritten information with the help of close() 1
  • 2. • Python automatically closes a file when the file’s reference object is reassigned to some other file. • Recommended to close the file explicitly. In [16]: f = open("test.txt","w+") print("File name :", f.name) print("File mode :", f.mode) print("File closed: ", f.closed) f.close() print("File closed: ", f.closed) File name : test.txt File mode : w+ File closed: False File closed: True 1.2 File Attributes 1. file.closed • If file is closed, it returns "True", else it returns "False" 2. file.name • It returns the file name 3. file.mode • It returns the mode in which the file was opened In [3]: myfile = open("test.txt","rb+") print("File Name : ", myfile.name) print("File mode : ", myfile.mode) print("File is closed or not :", myfile.closed) myfile.close() print("File is closed or not :", myfile.closed) File Name : test.txt File mode : rb+ File is closed or not : False File is closed or not : True 1.3 Reading and writing files 1.3.1 write() • for writing data into an open file • Syntax : – file_object.write(string) 2
  • 3. 1.3.2 read() • A string can be read from an open file with the help of read() • Syntax: – file_object.read([count]) * count : how many bytes are to be read from an open file • Reads the file from the beginning, if count is not provided 1.3.3 readline() and readlines() • Contents of a file can be read line-wise using readline() and readlines() • readline() – returns empty string when there is nothing more to read in a file. In [4]: # Writing into a file using write() file = open("test.txt","w+") file.write("Hello friends!! nLets do Python Programming!!") file.close() print('read file') file = open("test.txt",'a+') file.write("nBegin coding") file.close() read file In [5]: # Reading from a file using read() file = open("test.txt","r") str1 = file.read() print(str1) file.close() Hello friends!! Lets do Python Programming!! Begin coding In [6]: # Reading from a file using readline() f = open('test.txt') str1 = f.readline() while str1 != '': print(str1.strip('n')) str1 = f.readline() f.close() Hello friends!! Lets do Python Programming!! Begin coding 3
  • 4. In [7]: # Reading from a file using readlines() list1 = open('test.txt').readlines() for l in list1: print(l.strip('n')) print(list1) Hello friends!! Lets do Python Programming!! Begin coding ['Hello friends!! n', 'Lets do Python Programming!!n', 'Begin coding'] In [8]: # writelines()- is convenient to use when the data is available as a list of lines. f = open('test2.txt','w+') f.writelines(['an', 'bn', 'cn']) f.close() f = open('test.txt','r') for l in f.readlines(): print(l.strip('n')) f.close() Hello friends!! Lets do Python Programming!! Begin coding 1.4 File Positions • In Python, we can check and change the current positions of the file object with the help of the following functions 1. tell() – to know the current position in the file – it returns the current position in the form of an integer. 2. seek() – used to change the current position of the file – syntax: seek(offset [,from]) * offset : specifies the number of bytes to be moved * from : indicates the reference position, starting from where the bytes are to be moved. · 0, if reference position == file beginning · 1, if reference position == current file position · 2, if reference position == file end In [9]: myfile = open("test.txt","r+") str = myfile.read(10) print(str) pos = myfile.tell() print("Current File position = ",pos) 4
  • 5. Hello frie Current File position = 10 In [10]: str = myfile.read(20) print(str) pos = myfile.tell() print("Current File position = ",pos) myfile.seek(0,0) pos = myfile.tell() print("Current File position = ",pos) myfile.close() nds!! Lets do Pytho Current File position = 30 Current File position = 0 1.5 Rename and Remove Files • Done using the os module – rename() * used to change the name of the given file. * it takes two arguments, · current file name · new file name – remove() * used to delete files * takes one argument · file name In [11]: import os os.rename("test.txt","trial.txt") try: os.remove("test.txt") except FileNotFoundError: print("File does't exists") File does't exists 2 Directories • Files are collectively placed within different directories • Diectories can be organized using the built-in functions in os module. 5
  • 6. – mkdir("namedir") * to make a directory inside the current directory – getcwd() * to return the complete path of the current working directory – chdir("name dir") * to change the current working directory – rmdir("name") * to delete any directory In [30]: import os # help(os) c = os.getcwd() print("Current Working Directory : ", c) Current Working Directory : /home/lifna/Desktop/OST_Lab/Notebooks In [32]: os.mkdir("newdir") l = os.listdir() if "newdir" in l: print("Created new directory") else: print("Directory not present") print() print("After deleting Directory") os.rmdir("newdir") l = os.listdir() if "newdir" in l: print("Created new directory") else: print("Directory not present") Created new directory After deleting Directory Directory not present 3 Extra Programs 3.1 Write a program to list all files in the given directory. In [4]: import os p = "//home//lifna//Desktop//OST_Lab" l = os.listdir(p) for i in l: print(i) 6
  • 7. Perl pgms exception.py~ Python ppts .ipynb_checkpoints Perl ppts Python pgms Notebooks .Rhistory raise.py~ 3.2 Write a program extcount.py to count number of files for each extension in the given directory. • Input : directory name as argument • Execute : python extcount.py src/ • Output : print count and extension for each available file extension. 14 py 4 txt 1 csv In [67]: def ext_frequency(files): ext_files ={} for w in files: ext_files[w] = ext_files.get(w,0) + 1 return ext_files def main(): l = os.listdir() new_l = [] for i in range(0,len(l)-1): y =l[i].split('.') if y[1] != None: new_l.append(y[1]) frequency = ext_frequency(new_l) for ext, count in frequency.items(): print(count,ext) if __name__ =="__main__": main() 6 ipynb 2 pdf 1 ipynb_checkpoints 1 tex 7
  • 8. 3 png 4 txt 3.3 Write a program to list all the files in the given directory along with their length and last modification time. • Input : Directory name • Output : should contain one line for each file – filename length modification date • Hint: os.stat. In [44]: import os p = "//home//lifna//Desktop//OST_Lab//Notebooks" l = os.listdir(p) print('{:10}{:15}{:>60}'.format('Size','Modi_time','Name')) for i in l: #print(os.stat(i)[6]," ", os.stat(i)[8]," ",i) print('{:10}{:15}{:>60}'.format(os.stat(i)[6],os.stat(i)[8],i)) Size Modi_time Name 128030 1518583006 notebook.tex 70676 1517888227 raise.png 20503 1517908144 Exception Handling in Python.ipynb 37416 1518478223 Arrays in Python.ipynb 555 1518575856 Text Processing & Regular Expressions in Python.ipynb 152803 1518484088 Exception Handling in Python.pdf 28449 1518586881 File and Directories in Python.ipynb 6 1518580541 test2.txt 2380 1518478449 GUI Programming in Python.ipynb 65972 1517887221 finally.png 0 1518580725 test.txt 58061 1518483826 Arrays in Python.pdf 13169 1517977267 Lists in Python.ipynb 4096 1518575765 .ipynb_checkpoints 58 1518580536 trial.txt 45 1517892489 testfile 107336 1517886592 try_except.png 176 1518493459 she.txt 48 1517990309 text.txt 3.4 Write a program to print directory tree. • The program should take path of a directory as argument and print all the files in it recur- sively as a tree. • Execute : python dirtree.py foo 8
  • 9. • Hint : os.walk already does the top-down, depth-first walk foo |-- a.txt |-- b.txt |-- code | |-- a.py | |-- b.py | |-- docs | | |-- a.txt | | -- b.txt | -- x.py -- z.txt In [69]: import os def list_files(startpath): for root, dirs, files in os.walk(startpath): level = root.replace(startpath, '').count(os.sep) indent = ' ' * 4 * (level) print('{}{}/'.format(indent, os.path.basename(root))) subindent = ' ' * 4 * (level + 1) for f in files: print('{}{}'.format(subindent, f)) path = "//home//lifna//Desktop//OST_Lab" list_files(path) OST_Lab/ exception.py~ .Rhistory raise.py~ Perl pgms/ hello.pl Python ppts/ Python Text Processing.pdf Strings1.ppt Python_Files_2.pdf Database Python.pdf introduction-1.pptx 6_Arrays_84_Python.pdf Modules_Python.pdf Python Packages.pdf Python Files_1.pdf Python_img_proc.pdf Python Modules_2.pdf Module_1/ 2_1_2_Reading Writing Files.pdf 9
  • 10. Module 1 - Python Basics (Part 5).pdf raise.png 1_16_Advanced OOP.pdf Module 1 - Python Basics (Part 2).pdf 1_3_1_Input_Output.pdf Module 1 - Python Basics (Part 3).pdf 1_12_Customizing Classes and Overlaodong.pdf.crdownload 1_3_2_Input and Output.pdf Array_tables.odt 1_8_Lists_2.pdf 1_2_Basic Operators.pdf Module 1 - Python Basics (Part 4).pdf 1_7_Functions.pdf 1_10_Exception Handling_Final.pdf 1_8_Tuples.pdf 1_10_Exception Handling.pdf 1_8_Lists.pdf 1_4_2_Control Statements.pdf 1_0_Introduction to Python.pdf 2_5_6_ Text Processing and REgular Expressions.pdf 1_11_Introduction to OOP.pdf 1_4_1_Control Flow Statements.pdf 1_6_Strings and Characters.pdf 1_9_List_tuples_Dictionaries.pdf 1_12_13_Classes and Objects.pdf 2_1_File Processing.pdf 4_1_GUI.pdf 1_14_Abstract Classes and Interfaces.pdf 1_15_Inheriatance and Polymorphism.pdf beginners_python_cheat_sheet_pcc_all.pdf Module 1 - Python Basics (Part 1).pdf 1_1_DataTypes.pdf .ipynb_checkpoints/ GUI Programming in Python-checkpoint.ipynb File and Directories in Python-checkpoint.ipynb Lists in Python-checkpoint.ipynb Exception Handling in Python-checkpoint.ipynb Arrays in Python-checkpoint.ipynb Perl ppts/ Beginers Perl.pdf Perl_Day2.pdf Perl_Day1.pdf IO_Files_Directories.pdf Python pgms/ head_tail.py TimeClass.pyc TimeClass2.py~ lena.png 10
  • 11. TimeClass_spAttributes.py~ TimeClass.py~ face.png MyClass.pyc exception.py~ TimeClass_spAttributes.py TimeObject_spAttributes.py~ MyClass_protected.py~ raise.py Time2.py~ Time.py~ Time.py MyClass.py~ tail.py exception.py TimeObject_spAttributes.py Time2.py TimeClass2.pyc TimeClass2.py Time.pyc TimeClass.py chain_compare.py MyClass_protected.py chain_compare.py~ MyClass.py Notebooks/ notebook.tex raise.png Exception Handling in Python.ipynb Arrays in Python.ipynb Text Processing & Regular Expressions in Python.ipynb Exception Handling in Python.pdf File and Directories in Python.ipynb test2.txt GUI Programming in Python.ipynb finally.png test.txt Arrays in Python.pdf Lists in Python.ipynb trial.txt try_except.png she.txt text.txt .ipynb_checkpoints/ File and Directories in Python-checkpoint.ipynb Text Processing & Regular Expressions in Python-checkpoint.ipynb 11
  • 12. 3.5 Word Count • To compute the number of characters, words and lines in a file In [12]: # Number of characters in a file is same as the length of its contents. def charcount(filename): return len(open(filename).read()) # Number of words in a file can be found by splitting the contents of the file. def wordcount(filename): return len(open(filename).read().split()) # Number of lines in a file can be found from readlines method. def linecount(filename): return len(open(filename).readlines()) file = input("Enter the file name :").strip() char_count = charcount(file) print("Number of characters :",char_count) word_count = wordcount(file) print("Number of words :",word_count) line_count = linecount(file) print("Number of lines :", line_count) Enter the file name :trial.txt Number of characters : 58 Number of words : 8 Number of lines : 3 3.6 Word Frequency • To find number of occurrences of each word in a file. • Dictionary can be used to store the number of occurrences for each word. In [41]: # Lets first write a function to count frequency of words, given a list of words. def word_frequency(words): """ Returns frequency of each word given a list of words. word_frequency(['a', 'b', 'a']) {'a': 2, 'b': 1} """ frequency = {} for w in words: frequency[w] = frequency.get(w,0) + 1 return frequency 12
  • 13. # Getting words from a file is very trivial. def read_words(filename): return open(filename).read().split() # We can combine these two functions to find frequency of all words in a file. def main(filename): frequency = word_frequency(read_words(filename)) for word, count in frequency.items(): print(word, count) if __name__ =="__main__": # import sys # main(sys. argv[1]) fname = input("Enter the file name:") main(fname) Enter the file name:she.txt seashore, 1 seashore 1 I'm 2 if 1 seashore; 1 shells. 1 she 2 are 2 seashells 3 on 2 sure 1 The 1 shells 2 She 1 the 3 So 1 sells 3 sure. 1 that 2 3.7 Write a program reverse.py to print lines of a file in reverse order. • Input : she.txt – She sells seashells on the seashore; – The shells that she sells are seashells I’m sure. – So if she sells seashells on the seashore, – I’m sure that the shells are seashore shells. • Execute : python reverse.py she.txt • Output 13
  • 14. – I’m sure that the shells are seashore shells. – So if she sells seashells on the seashore, – The shells that she sells are seashells I’m sure. – She sells seashells on the seashore; In [13]: list1 = open("she.txt",'r').readlines() #print(list1) l = len(list1) list1.reverse() for l in list1: print(l,end='') I'm sure that the shells are seashore shells. So if she sells seashells on the seashore, The shells that she sells are seashells I'm sure. She sells seashells on the seashore; 3.8 Program to print each line of a file in reverse order. In [19]: def reverse(st): s = "" for i in st: s = i + s return s list1 = open("she.txt",'r').readlines() #print(list1) tlist = [] for l in list1: l = l.strip('n') #rev = reverse(l) print(reverse(l)) ;erohsaes eht no sllehsaes slles ehS .erus m'I sllehsaes era slles ehs taht sllehs ehT ,erohsaes eht no sllehsaes slles ehs fi oS .sllehs erohsaes era sllehs eht taht erus m'I 3.9 Implement unix commands head and tail. • The head and tail commands take a file as argument and prints its first and last 10 lines of the file respectively. In [ ]: ## Save the program as head_tail.py import sys def tail(fname, num_lines): 14
  • 15. with open(fname) as f: content = f.read().splitlines() count = len(content) for i in range(count-num_lines,count): print(content[i]) def head(fname, nlines): with open(fname) as f: content = f.read().splitlines() count = len(content) for i in range(0,nlines): print(content[i]) if len(sys.argv) !=3: print('Usage: tail.py <file> <nlines>') sys.exit(1) else: fname, nlines = sys.argv[1:] num_lines = int(nlines) try: f = open(fname,"r") if f == None: raise IOError else: print("First 10 lines in the file : ", fname) print("#################################") head(fname,num_lines) print() print() print("Last 10 lines in the file : ", fname) print("#################################") tail(fname,num_lines) except IOError: print("File doesn't exists in current working directory") 3.10 Write a program center_align.py to center align all lines in the given file. • Input : she.txt – She sells seashells on the seashore; – The shells that she sells are seashells I’m sure. – So if she sells seashells on the seashore, – I’m sure that the shells are seashore shells. • Execute : python center_align.py she.txt • Output : – I’m sure that the shells are seashore shells. 15
  • 16. – So if she sells seashells on the seashore, – The shells that she sells are seashells I’m sure. – She sells seashells on the seashore; In [27]: list1 = open("she.txt",'r').readlines() for l in list1: l = l.strip('n') print(l.center(60)) She sells seashells on the seashore; The shells that she sells are seashells I'm sure. So if she sells seashells on the seashore, I'm sure that the shells are seashore shells. 3.11 Write a function extsort to sort a list of files based on extension. • Input : extsort([’a.c’, ’a.py’, ’b.py’, ’bar.txt’, ’foo.txt’, ’x.c’]) • Output : [’a.c’, ’x.c’, ’a.py’, ’b.py’, ’bar.txt’, ’foo.txt’] In [15]: def extsort(x): i=0 while(i<len(x)): x[i]=x[i].split('.') i=i+1 x.sort(key=lambda x:x[1]) i=0 while(i<len(x)): x[i]=".".join(x[i]) i=i+1 return x print(extsort(['a.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt', 'x.c'])) ['a.c', 'x.c', 'a.py', 'b.py', 'bar.txt', 'foo.txt'] 3.12 Implement unix command grep. • Input : a string and a file as arguments – String : she – File : she.txt * She sells seashells on the seashore; * The shells that she sells are seashells I’m sure. * So if she sells seashells on the seashore, * I’m sure that the shells are seashore shells. • Execute : python grep.py she.txt sure • Output : prints all lines in the file which contain the specified string. 16
  • 17. – The shells that she sells are seashells I’m sure. – I’m sure that the shells are seashore shells. 3.13 Write a program wrap.py that takes filename and width as aruguments and wraps the lines longer than width. • Input : she.txt – She sells seashells on the seashore; – The shells that she sells are seashells I’m sure. – So if she sells seashells on the seashore, – I’m sure that the shells are seashore shells. • Execute : python wrap.py she.txt 30 • Output : – I’m sure that the shells are s – eashore shells. – So if she sells seashells on t – he seashore, – The shells that she sells are – seashells I’m sure. – She sells seashells on the sea – shore; 3.14 write a new program wordwrap.py that works like wrap.py, but breaks the line only at the word boundaries? • The above wrap program is not so nice because it is breaking the line at middle of any word. Can you • Input : she.txt – She sells seashells on the seashore; – The shells that she sells are seashells I’m sure. – So if she sells seashells on the seashore, – I’m sure that the shells are seashore shells. • Execute : python wordwrap.py she.txt 30 • Output : – I’m sure that the shells are – seashore shells. – So if she sells seashells on – the seashore, – The shells that she sells are – seashells I’m sure. – She sells seashells on the – seashore; 17