Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7512729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:57:34+00:00 2026-05-29T23:57:34+00:00

I am writing a wrapper to automate some android ADB shell commands via Python

  • 0

I am writing a wrapper to automate some android ADB shell commands via Python (2.7.2).
Since, in some cases, I need to run the command asynchronously, I am using the subprocess.Popen method to issue shell commands.

I have run into a problem with the formatting of the [command, args] parameter of the Popen method, where there require command/args split is different between Windows and Linux:

# sample command with parameters
cmd = 'adb -s <serialnumber> shell ls /system'

# Windows:
s = subprocess.Popen(cmd.split(), shell=False) # command is split into args by spaces

# Linux:
s = subprocess.Popen([cmd], shell=False) # command is a list of length 1 containing whole command as single string

I have tried using shlex.split(), with and with out the posix flag:

# Windows
posix = False
print shlex.split(cmd, posix = posix), posix
# Linux
posix = True
print shlex.split(cmd, posix = posix), posix

Both cases return the same split.

Is there a method in subprocess or shlex that handles the OS-specific formats correctly?

This is my current solution:

import os
import tempfile
import subprocess
import shlex

# determine OS type
posix = False
if os.name == 'posix':
    posix = True

cmd = 'adb -s <serialnumber> shell ls /system'
if posix: # posix case, single command string including arguments
    args = [cmd]
else: # windows case, split arguments by spaces
    args = shlex.split(cmd)

# capture output to a temp file
o = tempfile.TemporaryFile()
s = subprocess.Popen(args, shell=False, stdout=o, stderr=o)
s.communicate()
o.seek(0)
o.read()
o.close()

I don’t think shlex.split() is doing anything here, and cmd.split() achieves identical results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:57:35+00:00Added an answer on May 29, 2026 at 11:57 pm

    They seem to function identically when I turn off shell=True

    As per the docs:

    On Unix, with shell=True: If args is a string, it specifies the
    command string to execute through the shell. This means that the
    string must be formatted exactly as it would be when typed at the
    shell prompt. This includes, for example, quoting or backslash
    escaping filenames with spaces in them. If args is a sequence, the
    first item specifies the command string, and any additional items will
    be treated as additional arguments to the shell itself. That is to
    say, Popen does the equivalent of:

    Popen([‘/bin/sh’, ‘-c’, args[0], args[1], …])

    http://docs.python.org/library/subprocess.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Python wrapper for a C++ library, and I'm getting a really
Hi I writing a wrapper in c# and i have some problem. I have
I'm writing a wrapper around the ssh command line client. After the first positional
I am writing a wrapper for plot that automates some tasks that I find
I need to automate an command line application. It asks the user to enter
I am writing a wrapper for the ConfigParser in Python to provide an easy
I'm writing a wrapper in .NET (C++/CLI) to be able to use some native
I'm writing a wrapper around a C library in Objective-C. The library allows me
I'm writing a wrapper application that will be used to provide an abstraction layer
I'm writing a wrapper to a function that takes varargin as its inputs. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.