0% found this document useful (0 votes)
14 views16 pages

Python Session 19j Updated

This document outlines the objectives of automating common tasks using Python, including file handling and command line arguments. It explains how Python can automate tasks like importing files and accessing databases, while also detailing the use of the OS module for file operations and the sys.argv list for command line arguments. Additionally, it introduces the concept of pipes in Unix and Linux for chaining processes together.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Python Session 19j Updated

This document outlines the objectives of automating common tasks using Python, including file handling and command line arguments. It explains how Python can automate tasks like importing files and accessing databases, while also detailing the use of the OS module for file operations and the sys.argv list for command line arguments. Additionally, it introduces the concept of pipes in Unix and Linux for chaining processes together.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Automating Task and Using

Command Line Argument


Objectives

In this session, you will learn to:

Automate Common Tasks.


Use File Names and Paths.
Use Command Line Arguments and Pipes.
Automating Common
Tasks

Python helps in automating the tasks that are commonly


implemented by using a computer system.

Some of the tasks which can be automated by Python are

Importing .csv files.


Extracting data from XML
Controlling the browser.
Accessing SQL via SQLite.
File Names and Paths

Any program which runs inside the OS of the computer


system has a current directory.
It is also called as current working directory used by a Python
program to open and read the files.
OS module (Operating System module) provides a set of
functions which helps a programmer in performing various
operations related with working on files and directories.
Some of the important functions are :-
getcwd()
abspath()
exists()
isdir()
isfile()
Just a Minute

isdir() method
returns False if path is an
existing directory.
1. True
2. False
Just a Minute

isdir() method
returns False if path is an
existing directory.
1. True
2. False

Answer: False
File Names and Paths
(Contd.)

getcwd() - Returns the name of the current directory.

Example:-
File Names and Paths
(Contd.)

abspath() - Finds the absolute path to a file.

Example:-
File Names and Paths
(Contd.)

exists() - Checks whether a file or directory exist.

Example:-
Command Line
Arguments

Command line arguments help the user to pass the argument


during the execution of the program using command prompt.
Python also uses command line arguments to pass the
values, such as file name or any data for variables of the
program.
Inside the program these arguments are accessible through
the list variable sys.argv.
The name of the script is included in this list at sys.argv[0]
where as sys.argv[1] contains the first parameter, sys.argv[2]
the second and so on.
While executing the program from the command prompt, the
arguments are placed after the program name. The command
line arguments are separated by spaces.
Command Line
Arguments (Contd.)
Example:

import sys
print 'Count',len(sys.argv)
print ('Type',type(sys.argv))
for arg in sys.argv:
print('Argument',arg)

Execution :-
python argumente.py python course for beginners
Output :-
argumente.py
python
course
for
beginners
Just a Minute

Inside the program


command line arguments
are accessible through the
list variable _________.
Just a Minute

Inside the program


command line arguments
are accessible through the
list variable _________.

Answer: Sys.argv
Pipes

Pipe is an important concept used in Unix and Linux OS. Small


elements can be put together using Pipes and the processing
of these elements are chained together by standard streams.

A pipe is an object in Python which represents a running


process. Any program that we can launch from the shell can
also be launched from Python program using a pipe.

Example:

A DOS command "dir" normally displays the content of the


current directory. We can launch dir with os.popen() in
Python.
Activity

Activity : Handling Files Using Command Line Argument


Problem Statement:

Write a program to perform file operation using command line


arguments.

Prerequisite: For this activity please refer “images” folder available in


“Data_File_For_Students” folder.
Summary

 In this lesson, we learned:

Python can automate some of the tasks that commonly come across in
a daily routine.
A Python script can do a task which has to be done for hundreds of
files.
An operating System module provides the functions for working with
files and directories.
It is possible to pass some values from the command line to your
python programs when they are executed.
Parsing arguments is important for the program specially when there is
need to control from outside instead of hardcoding.
A pipe is an object that represents a running process.

You might also like