简体   繁体   中英

convert a bash script to python3

I have a bash script, which is running perfectly:

gvim  --servername "servername" $1
if [ -f ${1%.tex}.pdf ];
then
  evince ${1%.tex}.pdf &
fi
evince_vim_dbus.py  GVIM servername ${1%.tex}.pdf  $1 &

I am trying to convert it to python as:

#!/usr/bin/env python3

from subprocess import call
import sys, os

inp_tex = sys.argv[1]
oup_pdf = os.path.splitext(sys.argv[1])[0]+".pdf"
print(oup_pdf)

call(["gvim", "--servername", "servername",  sys.argv[1]])

if os.path.exists(oup_pdf):
  call(["evince", oup_pdf])

call(["evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])

in the python, both gvim and evince window is open, but evince_vim_dbus.py line is not working. Not that it is giving any error, but it is not showing intended result, as it should, and is doing with the bash script.

trying with check_call (I have to kill it after a while, here's the traceback):

Traceback (most recent call last):

  File "/home/rudra/vims.py", line 28, in <module>
    check_call(["python","/home/rudra/bin/evince_vim_dbus.py", "GVIM", "servername", oup_pdf, inp_tex])
  File "/usr/lib64/python3.5/subprocess.py", line 576, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib64/python3.5/subprocess.py", line 559, in call
    return p.wait(timeout=timeout)
  File "/usr/lib64/python3.5/subprocess.py", line 1658, in wait
    (pid, sts) = self._try_wait(0)
  File "/usr/lib64/python3.5/subprocess.py", line 1608, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt

I'm going to have a guess that your real problem isn't the evince_vim_dbus.py line itself, but rather the gvim line, because you pass it the server name 'servername' instead of simply servername , and so doesn't match the name on the line that runs evince_vim_dbus.py .

I'm not familiar with gvim or its server functionality, but I'm guessing the evince_vim_dbus.py program connects to gvim using the given name, in which case it's going to fail since the server of the right name isn't running.

If that's not it, then maybe the problem is that subprocess.call() runs the given program and waits for it to exit, whereas in your original bash script, you run evince with an ampersand, causing bash not to wait for it, so maybe the problem is that evince_vim_dbus.py never runs at all until you exit Evince.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM