【问题标题】:file not found error with python, no error in cmdpython找不到文件错误,cmd中没有错误
【发布时间】:2016-06-17 09:11:06
【问题描述】:

我为 windows 制作了一个 python 脚本来打印目录中的每个文件。为此,我使用 ghostscript 和 gsprint。

当我启动脚本时,出现错误WindowsError: [error2] file not found

如果我在 cmd 中启动相同的命令行,则没有错误并且打印 pdf...

这是我的脚本:

for f_name in files_in_dir:
    fullpath_file = os.path.join(base_print, path_print_source, f_name)
    carrier = f_name.split('-')[0]
    new_path = os.path.join(base_print, carrier + '_printed')
    new_fullpath_file = os.path.join(new_path, f_name)

    print Tools.format_txt("traitement du fichier %s" % f_name)
    print Tools.format_txt("traitement du fichier %s" % f_name, carrier)

    p = subprocess.Popen(['gsprint', '-printer', printer,'-dPDFFitPage', fullpath_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = p.communicate()
    if out:
        print Tools.format_txt(out)
    if err:
        print Tools.format_txt(err, level="error")

    print Tools.format_txt("fichier %s envoyé pour impression" % f_name, carrier)

    if not os.path.exists(new_path):
        os.makedirs(new_path)
    os.rename(fullpath_file, new_fullpath_file)

文件的生成路径为:C:\Users\GREG\Downloads\DHL-575555338523255087006833669.pdf

追溯:

Traceback (most recent call last):
  File "python-print.py", line 95, in <module>
    sys.exit(main())  # si le script est utilis├® comme un module, on n'ex├®cute pas le script
  File "python-print.py", line 61, in main
    p = subprocess.Popen(['gsprint', '-printer', printer,'-dPDFFitPage', fullpath_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 2] Le fichier spÚcifiÚ est introuvable

我正在使用带有 python 2.7 的 Windows 10

【问题讨论】:

  • 如何启动脚本? gsprint 在 PATH 中吗?
  • 是的 gsprint 在路径中
  • 脚本是在空闲状态下启动的——编辑:用 cmd 是一样的
  • 你能给出完整的回溯吗?
  • windows下应该不用gsprint.exe吧?

标签: python windows python-2.7 ghostscript


【解决方案1】:

经过多次尝试,我设法解决了这个问题:

Gsprint 安装在 c:\program files 我移动它并安装在 c:\GSP 所以一个没有空格的路径,它工作正常。

【讨论】:

  • PATH 目录中查找包含空格的文件没有问题。您是否错误地将 PATH 条目用双引号括起来? cmd.exe 是唯一一个忽略PATH 中的引号(有时)的程序,因为它实现了自己的搜索功能(继承自它的 OS/2 开头)。 Windows 中的几乎所有其他内容,包括CreateProcess,都直接或间接地依赖于SearchPath API,它试图将带有引号的目录列为路径的一部分。当然这会失败,它会悄悄地跳到下一个条目。
  • 是的,路径在引号之间(只有 gsprint 的路径,而不是整个字符串)。它是由 windows 生成的。
  • “它是由 Windows 生成的”是什么意思?你的意思是ghostscript的安装程序添加了这个伪造的PATH条目?
  • 在 Windows 10 上,当您编辑路径时,它会打开一个包含所有目录列表的窗口,如果您想添加一个,只需浏览到该目录并单击添加。它不是 Windows 7 上的基本文本编辑(此处为图片:comtek4u.com/sites/default/files/images/…
  • 我已经使用了那个新对话框。它不仅用于PATH,还用于任何具有以分号分隔的 2 个或更多完全限定路径列表的变量。它在对话框中围绕路径添加的引号实际上并未添加到最终的PATH 中,至少对我来说不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2017-07-03
  • 2018-04-10
  • 1970-01-01
相关资源
最近更新 更多