3.3 Format Operator and Command Line
3.3 Format Operator and Command Line
tell() method which is used to print the byte number at which the file pointer currently
exists. PROGRAM 8
fileptr = open("file1.txt","r")
content = fileptr.read();
#after the read operation file pointer modifies. tell() returns the location of the fileptr.
OUTPUT:
Syntax:
<file-ptr>.seek(offset[, from)
offset: It refers to the new position of the file pointer within the file.
from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the
beginning of the file is used as the reference position. If it is set to 1, the current position of the file
pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the reference
position.
PROGRAM 9
Write a program to set the file pointer in different location.
fileptr = open("file1.txt","r")
fileptr.seek(4);
#read Content
text=fileptr.read()
print(text)
#read content
fileptr.seek(2);
text=fileptr.read()
print(text)
OUTPUT
FILE ATTRIBUTES
PROGRAM 10
fileptr = open("file1.txt","r")
print("File Name " , fileptr.name)
OUTPUT
The Python os module enables interaction with the operating system. The os module provides the
functions that are involved in file processing operations like renaming, deleting, etc. It provides us the
rename() method to rename the specified file to a new name.
Syntax:
rename(current-name, new-name)
The first argument is the current file name and the second argument is the modified name. We can change
the file name bypassing these two arguments.
PROGRAM 11
import os
fileptr = open("file1.txt","x")
os.rename("file1.txt","file3.txt")
REMOVING THE FILE
The os module provides the remove() method which is used to remove the specified file. The syntax to
use the remove() method is given below.
remove(file-name)
PROGRAM 12
os.remove("file1.txt")
Command line arguments are input parameters passed to the script when executing them.
⮚ Python sys.argv
Python sys module stores the command line arguments into a list, we can access it using sys.argv. This is
very useful and simple way to read command line arguments as String.
PROGRAM 13
import sys
print(type(sys.argv))
for i in sys.argv:
print(i)
OUTPUT
PYTHON GETOPT MODULE
Python getopt module is very similar in working as the C getopt() function for parsing command-line
parameters. Python getopt module is useful in parsing.
PROGRAM 14
import getopt
import sys
argv = sys.argv[1:]
try:
print(opts)
print(args)
except getopt.GetoptError:
sys.exit(2)
OUTPUT
PYTHON ARGPARSE MODULE
Python argparse module is the preferred way to parse command line arguments. It provides a lot of option
such as positional arguments, default value for arguments, help message, specifying data type of
argument etc. It means to communicate between the writer of a program and user which does not require
going into the code and making changes to the script. It provides the ability to a user to enter into the
command-line arguments.
Syntax:
Parameters:
options: String of option letters that the script want to recognize. Options that require an argument should
be followed by a colon (:).
long_options: List of string with the name of long options. Options that require arguments should be
followed by an equal sign (=).
Return Type: Returns value consisting of two elements: the first is a list of (option, value) pairs. The
second is the list of program arguments left after the option list was stripped.
Program 15
import sys
# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)
# Arguments passed
# Addition of numbers
Sum = 0
# Using argparse module
Sum += int(sys.argv[i])
print("\n\nResult:", Sum)
OUTPUT