File Input Output�Python
Satishkumar L. Varma
Professor, Department of Computer Engineering
PCE, New Panvel
www.sites.google.com/site/vsat2k
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
2
Function – Print: Print to the Screen
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
3
Reading Keyboard Input
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
4
Opening and Closing Files
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
file object = open(file_name [, access_mode][, buffering])
5
open() function Paramters
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
6
Modes of Opening a File
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
7
Modes | Description |
r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
rb | Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. |
r+ | Opens a file for both reading and writing. The file pointer will be at the beginning of the file. |
rb+ | Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. |
w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
wb | Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
w+ | Opens a file for both writing and reading. Overwrites the existing file if file exists. If the file does not exist, creates a new file for reading and writing. |
Modes of Opening a File
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
8
Modes | Description |
wb+ | Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. |
a | Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
ab | Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. |
a+ | Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
ab+ | Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
open() and close() File Object Attributes
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
fileObject.close()
9
Attribute | Description |
file.closed | Returns true if file is closed, false otherwise. |
file.mode | Returns access mode with which file was opened. |
file.name | Returns name of the file. |
Example: openCloseFile.py
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
# Code:
f = open("testFile.txt", "wb")
print("Name of the file : ", f.name)
print("Closed or not : ", f.closed)
print("Opening mode : ", f.mode)
f.close()
print("Closed or not : ", f.closed)
# Output:
Name of the file : testFile.txt
Closed or not : False
Opening mode : wb
Closed or not : True
10
Reading From File
f = open("myDocument.txt", "r")
str1 = f.read()
#str2 = f.readline()
#str3 = f.readlines()
print(str1)
f.close()
11
Writing into File
myList = ['abc', 'xyz', 'lmn', ' ok.' ]
f = open("myDocument1.txt", "w")
#str1 = f.read()
for i in myList:
f.write(i)
f.close()
12
For Loop and File Input
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
# For Loop and File Input
import fileinput
for line in fileinput.input('myDocument.txt'):
print(line)
13
Piping with Files
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
# Code:
import fileinput
for i in fileinput.input():
print(i)
14
Iterating over File Contents
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
# Code
for i in open('myDocument.txt'):
print(i)
15
File Positions
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
16
File Positions
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
# Example of seek()
f = open("myDocument.txt", "r+")
str = f.read(15);
print("Read String is : ", str)
position = f.tell();
print("Current file position : ", position)
position = f.seek(0, 0);
str = f.read(15);
print("Again read String is : ", str)
f.close()
17
Renaming Files
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
import os
os.rename( "test1.txt", "test2.txt" )
18
Deleting Files
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
os.remove(file_name)
import os
os.remove("test2.txt")
19
Directories in Python
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
os.mkdir("newdir")
import os # Create a directory "test"
os.mkdir("test")
20
Directories in Python
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
os.chdir("newdir")
import os
os.chdir("/home/newdir")
21
Directories in Python
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
os.getcwd()
import os
os.getcwd()
22
Directories in Python
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
os.rmdir('dirname')
import os
os.rmdir( "/tmp/test" )
23
File & Directory Related Methods
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
24
Outline
Satishkumar L. Varma, PCE New Panvel www.sites.google.com/site/vsat2k
25
References
26
Satishkumar Varma, PCE www.sites.google.com/site/vsat2k
Thank You.
Satishkumar Varma, PCE www.sites.google.com/site/vsat2k
27