【发布时间】:2013-11-08 21:03:21
【问题描述】:
我在许多(python-django)模板中重命名了一个 css 类名。然而,css 文件广泛分布在多个目录中的多个文件中。我有一个 python sn-p 从根目录开始重命名,然后递归地重命名所有 css 文件。
from os import walk, curdir
import subprocess
COMMAND = "find %s -iname *.css | xargs sed -i s/[Ff][Oo][Oo]/bar/g"
test_command = 'echo "This is just a test. DIR: %s"'
def renamer(command):
print command # Please ignore the print commands.
proccess = subprocess.Popen(command.split(), stdout = subprocess.PIPE)
op = proccess.communicate()[0]
print op
for root, dirs, files in walk(curdir):
if root:
command = COMMAND % root
renamer(command)
它不起作用,给出:
find ./cms/djangoapps/contentstore/management/commands/tests -iname *.css | xargs sed -i s/[Ee][Dd][Xx]/gurukul/g
find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find ./cms/djangoapps/contentstore/views -iname *.css | xargs sed -i s/[Ee][Dd][Xx]/gurukul/g
find: paths must precede expression: |
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
当我复制并运行相同的命令(如上所示)时,find 不会出错,并且 sed 要么没有输入文件,要么可以正常工作。
python sn-p 出了什么问题?
【问题讨论】:
-
您需要使用
shell=True才能使管道正常工作。例如,请参阅this answer。 -
在这种情况下你不需要管道:
op = subprocess.check_output(["find", root]+ r"-iname \*.css -exec sed -i s/foo/bar/gi {} +".split())
标签: python sed find subprocess xargs