我正在尝试使用 ghostscript 从 Django 内部将 pdf 转换为 jpg,但是当我尝试使用 subprocess.popen 运行 ghostscript 命令时,出现“找不到文件”错误。如果我通过命令行从 py 文件运行完全相同的脚本,它可以正常工作。
这是我的脚本:
import os
import subprocess
path = "/tmp/e15hntd/"
### this next line is what seems to cause the file not found error
sp = subprocess.Popen("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + path + "/static.jpg " + path + "/source.pdf")
在四处寻找可能的解决方案后,我尝试了几个替代版本的违规行,例如:
sp = subprocess.Popen(['gs', '-dSAFER', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-sDEVICE=jpeg', '-sOUTPUTFILE=' + path + "/static.jpg", path + "/source.pdf"])
或者:
sp = subprocess.call("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + path + "/static.jpg " + path + "/source.pdf")
这是完整的错误:
[Errno 2] 没有这样的文件或目录
追溯:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in wrapper
366. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
89. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py" in inner
196. return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapper
25. return bound_func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in _wrapped_view
91. response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py" in bound_func
21. return func(self, *args2, **kwargs2)
File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py" in inner
209. return func(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py" in change_view
1054. self.save_model(request, new_object, form, True)
File "/home/Django/Update/admin.py" in save_model
41. sp = subprocess.call("gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=" + unique_path + "/static.jpg " + unique_path + "/source.pdf" + unique_path + "/source.pdf")
File "/usr/lib/python2.6/subprocess.py" in call
480. return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py" in __init__
633. errread, errwrite)
File "/usr/lib/python2.6/subprocess.py" in _execute_child
1139. raise child_exception