Python Shlex Functions

Python Shlex Functions

                                                   Python Shlex Functions
Hello ,This is Rohit Kumar. In this article i will talk about python shlex functions.
The shlex module defines the following functions:
1.shlex.split(s, comments=False, posix=True):
Split the string s using shell-like syntax. If comments is False (the default), the parsing of comments in the given string will be disabled (setting the commenters attribute of the shlex instance to the empty string). This function operates in POSIX mode by default, but uses non-POSIX mode if the posix argument is false.
Note:Since the split() function instantiates a shlex instance, passing None for s will read the string to split from standard input.
# First we import module shlex
import shlex
s='somefile; rm -rf ~'

            ['somefile;', 'rm', '-rf', '~']
Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.

This idiom would be unsafe:

>>> filename = 'somefile; rm -rf ~'
>>> command = 'ls -l {}'.format(filename)
>>> print(command) # executed by a shell: boom!
ls -l somefile; rm -rf ~

quote() lets you plug the security hole:

>>> command = 'ls -l {}'.format(quote(filename))
>>> print(command)
ls -l 'somefile; rm -rf ~'
>>> remote_command = 'ssh home {}'.format(quote(command))
>>> print(remote_command)
ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''

import shlex
s='somefile; rm -rf ~'


        'somefile; rm -rf ~'


Concatenate the tokens of the list split_command and return a string. This function is the inverse of split().
from shlex import join
print(join(['echo', '-n', 'Multiple words']))
echo -n 'Multiple words'


More Articles of Rohit Kumar:

Name Views Likes
Python Copy ShallowCopy 323 4
Python PyGreSQL Classic PyGreSQL Interface Advanced Features 409 3
Python Nested Functions 398 3
Python PyGreSQL Classic PyGreSQL Interface DB Wrapper Class 475 3
Python Event Loop 391 4
Python PyGreSQL DB-API PyGreSQL Interface Functions 441 3
Python Shlex get_token() Method 681 4
Python Class Method Use 420 4
Python PyGreSQL Classic PyGreSQL Interface System Catalogs Examples 436 3
Python Multiple Vs Multi-level Inheritence 582 4
Python Shlex error_leader() Method 401 4
Python PyGreSQL Classic PyGreSQL Interface LargeObject 396 3
Python Bisect Insertion Functions 267 4
Python Multiple Inheritence 370 3
Python shlex class 499 4
Python Class Method 404 4
Python Shlex Quoted String 569 3
Python Shlex Methods 461 4
Python PyGreSQL Introduction 637 4
Python PasswordGenerator Project 768 4
Python Shlex Difference between Non-Posix and Posix Parsing 481 3
Python Shlex Functions 870 4
Python Shlex Split Example 520 3
Python PyGreSQL DB-API PyGreSQL Interface Cursor Object 447 3
Python Shlex Error-Handling 479 4
Python Copy deepcopy() 343 4
Python Shlex shlex.split() Vs re.split() 742 3
Python Server Program 747 4
Python Thumb Rule 636 4
Python Class Method Vs Static Method 535 4
Python PyGreSQL Classic PyGreSQL Interface Connection Object 476 4
Python PyGreSQL DB-API 2.0 Interface 465 4
Python PyGreSQL DB-API PyGreSQL Interface Connection Object 400 3
Python Bisect Functions 325 3
Python Loop Types 361 4
Python Bisect Introduction 472 4
Python Copy Introduction 310 4
Python PyGreSQL Classic PyGreSQL Interface Functions 457 3
Python PyGreSQL Classic PyGreSQL Interface SQL Functions Examples 415 3
Python Shlex Introduction 600 4
Python PyGreSQL Classic PyGreSQL Interface Basic Example 644 3
Python Shlex read_token() Method 505 3
Python Copy Shallowcopy Vs Deepcopy 295 4