1

Is there a way, I can execute in python a bash command with expansion: *

I tried thousand ways with no luck.

Actually I want to have a python script which enters each directory in the current dir, and executes a given bash command there (possibly a bash command with an expansion: *).

4

3 回答 3

3
import os
from subprocess import check_call

cmd = 'echo *' # some shell command that may have `*`
for dirname in filter(os.path.isdir, os.listdir(os.curdir)):
    check_call(cmd, shell=True, cwd=dirname)
  • filter(os.path.isdir, os.listdir(os.curdir)) lists all subdirectories of the current directory including those that starts with a dot (.)
  • shell=True executes command given as cmd string through the shell. * if present is expanded by the shell as usual
  • cwd=dirname tells that the command should be executed in dirname directory
于 2012-03-03T16:27:08.373 回答
1

Would you maybe have use for the glob module?

>>> import glob
>>> glob.glob("*")
['build', 'DLLs', 'Doc', 'ez_setup.py', 'foo-bar.py', 'include', 'Lib', 'libs','LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Removesetuptools.exe', 'Scripts', 'selectitems.py', 'selectitems.pyc', 'setuptools-wininst.log', 'share', 'so_vector.py', 'tcl', 'Tools', 'w9xpopen.exe']
>>>
于 2012-03-03T16:17:23.673 回答
1

Since you're going to have the shell execute the command, let the shell do the expansion of the shell metacharacters. You can run:

sh -c "your_commaand -with *"

The shell will process the globbing for you and execute the command.

That leaves you with the problem of traversing the subdirectories of the current directory. There must be a Python module to do that.

If you decide your program should chdir() to the sub-directories, you must be careful to come back to the starting directory after processing each one. Alternatively, the shell can deal with that for you, too, using:

sh -c "cd relevant-subdir; your_command -with *"

This avoids problems because the shell is a separate process switching directories without affect your main Python process.

于 2012-03-03T16:37:28.290 回答