我怎样才能 运行 这个 shell 脚本在 python 里面?
How can I run this shell script inside python?
我想 运行 来自 python 程序的 bash 脚本。该脚本有这样的命令:
find . -type d -exec bash -c 'cd "[=13=]" && gunzip -c *.gz | cut -f 3 >> ../mydoc.txt' {} \;
通常我会 运行 子进程调用,例如:
subprocess.call('ls | wc -l', shell=True)
但是由于引号的缘故,这在这里是不可能的。有什么建议吗?
谢谢!
用 \
.
转义 '
标记
即 对于每个:'
,替换为:\'
三重引号或三重双引号('''some string''' 或 """some other string""")也很方便。请参阅 here(是的,它的 python3 文档,但它在 python2 中 100% 有效)
mystring = """how many 'cakes' can you "deliver"?"""
print(mystring)
how many 'cakes' can you "deliver"?
虽然问题已经得到回答,但我仍然会插话,因为我假设您想执行那个 bash 脚本,因为您没有功能上等效的 Python 代码(这是基本上超过 40 行,见下文)。
为什么要这样做而不是 bash 脚本?
- 您的脚本现在能够 运行 在任何具有 Python 解释器
的 OS 上
- 功能更易于阅读和理解
- 如果您需要任何特殊的东西,调整您自己的代码总是更容易
- 更多 Pythonic :-)
请记住(作为您的 bash 脚本)没有任何类型的错误检查,输出文件是一个全局变量,但可以轻松更改。
import gzip
import os
# create out output file
outfile = open('/tmp/output.txt', mode='w', encoding='utf-8')
def process_line(line):
"""
get the third column (delimiter is tab char) and write to output file
"""
columns = line.split('\t')
if len(columns) > 3:
outfile.write(columns[3] + '\n')
def process_zipfile(filename):
"""
read zip file content (we assume text) and split into lines for processing
"""
print('Reading {0} ...'.format(filename))
with gzip.open(filename, mode='rb') as f:
lines = f.read().decode('utf-8').split('\n')
for line in lines:
process_line(line.strip())
def process_directory(dirtuple):
"""
loop thru the list of files in that directory and process any .gz file
"""
print('Processing {0} ...'.format(dirtuple[0]))
for filename in dirtuple[2]:
if filename.endswith('.gz'):
process_zipfile(os.path.join(dirtuple[0], filename))
# walk the directory tree from current directory downward
for dirtuple in os.walk('.'):
process_directory(dirtuple)
outfile.close()
我想 运行 来自 python 程序的 bash 脚本。该脚本有这样的命令:
find . -type d -exec bash -c 'cd "[=13=]" && gunzip -c *.gz | cut -f 3 >> ../mydoc.txt' {} \;
通常我会 运行 子进程调用,例如:
subprocess.call('ls | wc -l', shell=True)
但是由于引号的缘故,这在这里是不可能的。有什么建议吗?
谢谢!
用 \
.
'
标记
即 对于每个:'
,替换为:\'
三重引号或三重双引号('''some string''' 或 """some other string""")也很方便。请参阅 here(是的,它的 python3 文档,但它在 python2 中 100% 有效)
mystring = """how many 'cakes' can you "deliver"?"""
print(mystring)
how many 'cakes' can you "deliver"?
虽然问题已经得到回答,但我仍然会插话,因为我假设您想执行那个 bash 脚本,因为您没有功能上等效的 Python 代码(这是基本上超过 40 行,见下文)。 为什么要这样做而不是 bash 脚本?
- 您的脚本现在能够 运行 在任何具有 Python 解释器 的 OS 上
- 功能更易于阅读和理解
- 如果您需要任何特殊的东西,调整您自己的代码总是更容易
- 更多 Pythonic :-)
请记住(作为您的 bash 脚本)没有任何类型的错误检查,输出文件是一个全局变量,但可以轻松更改。
import gzip
import os
# create out output file
outfile = open('/tmp/output.txt', mode='w', encoding='utf-8')
def process_line(line):
"""
get the third column (delimiter is tab char) and write to output file
"""
columns = line.split('\t')
if len(columns) > 3:
outfile.write(columns[3] + '\n')
def process_zipfile(filename):
"""
read zip file content (we assume text) and split into lines for processing
"""
print('Reading {0} ...'.format(filename))
with gzip.open(filename, mode='rb') as f:
lines = f.read().decode('utf-8').split('\n')
for line in lines:
process_line(line.strip())
def process_directory(dirtuple):
"""
loop thru the list of files in that directory and process any .gz file
"""
print('Processing {0} ...'.format(dirtuple[0]))
for filename in dirtuple[2]:
if filename.endswith('.gz'):
process_zipfile(os.path.join(dirtuple[0], filename))
# walk the directory tree from current directory downward
for dirtuple in os.walk('.'):
process_directory(dirtuple)
outfile.close()