0% found this document useful (0 votes)
3 views7 pages

3.3 Format Operator and Command Line

The document provides an overview of file pointer operations in Python, including methods like tell() to get the current position and seek() to change the position of the file pointer. It also covers file attributes, renaming files using the os module, and removing files. Additionally, it discusses command line arguments and how to handle them using the sys, getopt, and argparse modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

3.3 Format Operator and Command Line

The document provides an overview of file pointer operations in Python, including methods like tell() to get the current position and seek() to change the position of the file pointer. It also covers file attributes, renaming files using the os module, and removing files. Additionally, it discusses command line arguments and how to handle them using the sys, getopt, and argparse modules.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

FILE POINTER POSITIONS

tell() method which is used to print the byte number at which the file pointer currently

exists. PROGRAM 8

Write a program to display the file position pointer.

# open the file file2.txt in read mode

fileptr = open("file1.txt","r")

#initially the filepointer is at 0

print("The filepointer is at byte :",fileptr.tell())

#reading the content of the file

content = fileptr.read();

#after the read operation file pointer modifies. tell() returns the location of the fileptr.

print("After reading, the filepointer is at:",fileptr.tell())

OUTPUT:

MODIFYING FILE POINTER POSITION


To change the file pointer location externally since we may need to read or write the content at various
locations.Python provides us the seek() method which enables us to modify the file pointer position
externally.

The syntax to use the seek() method is given below.

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.

# open the file file2.txt in read mode

fileptr = open("file1.txt","r")

#initially the filepointer is at 0

print("The filepointer is at byte :",fileptr.tell())

#changing the file pointer location to 10.

fileptr.seek(4);

#tell() returns the location of the fileptr.

print("After reading, the filepointer is at:",fileptr.tell())

#read Content

text=fileptr.read()

print(text)

#read content

fileptr.seek(2);

text=fileptr.read()

print(text)

OUTPUT

FILE ATTRIBUTES
PROGRAM 10

Write a program to to display the file attributes

# open the file file1.txt in read mode

fileptr = open("file1.txt","r")
print("File Name " , fileptr.name)

print("Mode of file " , fileptr.mode)

print("File closed " , fileptr.close)

OUTPUT

RENAMING THE FILE

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

Write a program to rename the file.

import os

# open the file file1.txt in read mode

fileptr = open("file1.txt","x")

#rename file1.txt to file3.txt

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

Write a program to remove the file


import os;

#deleting the file named file1.txt

os.remove("file1.txt")

COMMAND LINE ARGUMENTS

Command line arguments are input parameters passed to the script when executing them.

⮚ Python sys.argv

⮚ Python getopt module

⮚ Python argparse module

PYTHON SYS MODULE

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

Write a program to display the inputs using CLA

import sys

print(type(sys.argv))

print('The command line arguments are:')

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:

opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file='])

print(opts)

print(args)

except getopt.GetoptError:

# Print a message or do something useful

print('Something went wrong!')

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:

getopt.getopt(args, options, [long_options])

Syntax: getopt.getopt(args, options, [long_options])

Parameters:

args: List of arguments to be passed.

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

Find Sum of elements using Command Line arguments

import sys

# total arguments

n = len(sys.argv)
print("Total arguments passed:", n)

# Arguments passed

print("\nName of Python script:",

sys.argv[0]) print("\nArguments passed:",

end = " ") for i in range(1, n):

print(sys.argv[i], end = " ")

# Addition of numbers

Sum = 0
# Using argparse module

for i in range(1, n):

Sum += int(sys.argv[i])

print("\n\nResult:", Sum)

OUTPUT

You might also like