我想在bash脚本中将某些PDF文件转换为TXT文件。如何在Python脚本中运行bash命令?
pdf2txt.py -o otuput.txt input.pdf
这是做任务的单个文件的命令。 但是对于大型文件集,我试图执行以下操作。
#!/bin/bash
cd /home/z..../P...../file/pdf
python << END
import os
file_lst = os.listdir(r'/home/z..../P...../file/pdf')
out_file_lst = []
l = len(file_lst)
for i in file_lst:
out_file_lst.append(file_lst[0].split('.')[0] + '.txt')
for i in range(l):
pdf2txt.py -o out_file_lst[i] file_lst[i]
# How to run this bash command inside of python ?
END
此以前已经回答了。例如,请参阅http://stackoverflow.com/questions/4256107/running-bash-commands-in-python或http://stackoverflow.com/questions/20415522/running-a-bash-script-from-python – 2014-10-07 14:57:02
感谢您的建议,但那不是我所期待的。 – 2014-10-07 16:17:03
为什么你首先将这个作为bash脚本?你有一个Python脚本,唯一''bash'命令('cd ...')可以通过'os.chdir'移动到Python脚本中。或者,不要使用Python将整个事物变成“bash”脚本。像这样的交替语言没有任何用处。 – chepner 2014-10-07 16:52:08