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
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