U5L1
U5L1
Introduction :
• These files can be used whenever required and may be reopened, modified and copied.
• File can be accessed either by absolute file names or relative file names.
• Absolute File Name: A File name which can specify all the directory names starting from
the root of the tree.
• Example: D:\python\unit5.txt
Text File:
• A file can be processed (i.e., read, write, modify) using text editor such as notepad is called
text file.
• Text file are stored in a form that is human readable.
Binary File:
• All other files other than text files are called binary file.
• A binary file is a file stored in binary format.
• A binary file is a computer readable but not human readable.
File operations: -
• To read or write a file, it is necessary to open it using Python’s built-in function named
open () function.
• The file object is used to get various information related to that file.
Attribute Description
file.mode Returns the file access mode with which file was opened.
file.name Returns name of the file.
Example: -
Output:-
Name of the file: file1.txt
Closed or not: False
Opening mode: w+
The close () Method: -
• The function close () of a file object flushes any unwritten information and closes the file
object when there is no more writing to be done.
• Syntax: fileobject.close ()
• The write () method is used to write any string to a file which is opened.
Syntax: fileobject.write(string)
Example: -
fobj = open("file1.txt","w+") Output: -
str1="Hello python"
file1.txt
fobj.write(str1)
fobj.close () Hello python
The read () Method: -
• The file read() function reads the file contents from an open file.
• Syntax: fileobject.read([count])
• The argument passed is the number of bytes to be read from the opened file.
• This method starts reading from the beginning of the file and if the argument count is
missing, then it tries to read as much as possible, till the end of file.
Example:
f = open("file1.txt", "w+")
str1="Python is a programming language"
f.write(str1)
f.close()
f = open("file1.txt", "r+")
str2= f.read (20)
print("The string read is: ",str2)
f.close()
Output: -
The string read is: Python is a programm
File Positions: -
• The tell() method gives the current object position within the file.
• Syntax:
• os. rename (current filename, new filename)
• Example:
• Following is the example to rename an existing file file1.txt:
import os
os. rename (“file1.txt”, “file2.txt”)
The remove () Method: -
• The remove () method can be used to delete files by supplying the name of the file to be
deleted as the argument.
Syntax:
• os.remove (file name)
Output:
• 15
Example: 2
• print (“%d%d%d” % (1,2))
Output:
• TypeError: not enough arguments for format string
Example:3
• print (“%d” %” hello”)
Output:
• TypeError: %d format: a real number is required, not str
COMMAND LINE ARGUMENTS
• Command line arguments are what we type at the command line prompt along with the
script name while we try to execute our scripts.
• Python like most other languages provides this feature. sys.argv is a list in Python, which
contains the command line arguments passed to the script.
• We can count the number of arguments using len (sys. argv) function.
To use sys. argv, we have to import the sys module.
import sys
print("No. of arguments:",len (sys. argv))
print("Argument List:”, str (sys. argv))
Output: -
No. of arguments: 1
Argument List:
['C:/Users/ManiKandan/AppData/Local/Programs/Python/Python310/12
.py']
Question Time
File
1. _____are used to store data permanently.
s
2. In python, a file is classified as either
Text__________or_________.
Binary
file file
tell(
3. The _________method gives the current object position within the file.
)
remove
4. The ___________method can be used to delete files.
()
sys.arg
5. _____________is a list in Python, which contains the command line arguments passed to
v
the script.
Thank You