-1

I want to convert some PDF file to TXT file in a bash script.

pdf2txt.py -o otuput.txt input.pdf

this is the command to do the task for a single file. But for large set of file I am trying to do the following.

#!/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
Md. Zakir Hossan
  • 517
  • 2
  • 6
  • 17
  • This has been answered previously. See, for example, http://stackoverflow.com/questions/4256107/running-bash-commands-in-python or http://stackoverflow.com/questions/20415522/running-a-bash-script-from-python – Max Murphy Oct 07 '14 at 14:57
  • Thanks for your suggestion but that is not I am looking for. – Md. Zakir Hossan Oct 07 '14 at 16:17
  • 1
    Why are you making this a `bash` script in the first place? You have a Python script, and the only `bash` command (`cd ...`) can be moved into the Python script anyway using `os.chdir`. Alternately, make the whole thing a `bash` script without using Python. Alternating languages like this serves no purpose. – chepner Oct 07 '14 at 16:52
  • @Md.ZakirHossan - you are specifically asking how to run a bash command from python, and http://stackoverflow.com/questions/4256107/running-bash-commands-in-python and http://stackoverflow.com/questions/20415522/running-a-bash-script-from-python show you exactly how to do it - all you need to do is plugin your specific command. – Tony Suffolk 66 Oct 07 '14 at 17:02

2 Answers2

3

You can create a bin/bash script that convert all pdf files in your directory in txt files.

#!/bin/bash
for file in *.pdf;
do pdftotext "$file" "$file.txt";
done
Ellipticat
  • 198
  • 1
  • 4
  • 11
0

I strongly suggest subprocess lib.

For example:

return_code = subprocess.call(['ls', '-l'])
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93