Python 子进程无法访问 .bat 文件,因为它被另一个进程使用

Posted

技术标签:

【中文标题】Python 子进程无法访问 .bat 文件,因为它被另一个进程使用【英文标题】:Python subprocess cannot access the .bat file because it's used by another process 【发布时间】:2014-04-07 15:41:14 【问题描述】:

我编写了一个 python 脚本,该脚本创建了一个 Windows 批处理文件,用于标记 PDF 文件。

批处理程序运行良好,如果在 Windows 中打开/运行它,例如,通过选择它并点击 Enger。

但是当我尝试在批处理文件上运行 subprocess 时,我收到以下错误消息:

 Traceback (most recent call last):
   File "C:\Apps\UtilitiesByMarc\bates_stamp_pdf_files_using_veryPDF_and_regex_match_groups_and_verbose_.py", line 56, in <module>
     RetVal = subprocess.call(target_file_fullname, shell=False)
   File "C:\Python27\lib\subprocess.py", line 493, in call
     return Popen(*popenargs, **kwargs).wait()
   File "C:\Python27\lib\subprocess.py", line 679, in __init__
     errread, errwrite)
   File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
     startupinfo)
 WindowsError: [Error 32] The process cannot access the file because it is being used by another process

我不知道如何以防止错误消息发生的方式运行子进程。任何建议都将不胜感激,尤其是对问题解释的参考。

这是python代码:

 # -*- coding: latin-1 -*-
 #  bates_stamp_pdf_files_using_veryPDF_.py

 import os
 import sys
 import re
 import time
 import subprocess
 exhibits_temp_dir = r'\\Hpquadnu\c\Svr1\K\data\Steinberg Nisan\Scans.Steinberg Nisan\Exhibits.Temp'
 stamp_prg = r'"C:\Apps\pdfstamp_cmd_veryPDF\pdfstamp.exe"'

 # DOES NOT WORK; VERBOSE MB DONE IN THE COMPILE STATEMENT srchptrn = (r"""([exEXapAPatAT]2,3_?) # Ex or Att or App, etc.
 #             ([0-9]+)                   # Seq. no of the document
 #             ([^0-9]+.+)                # a non-digit character to separate the seq. no from other identifying chars.""", re.X)

 srch_or_match_obj = re.compile(r"""([exEXapAPatAT]2,3_?)     # Ex or Att or App, etc.
                                     ([0-9]+)                   # Seq. no of the document
                                     ([^0-9]+.+)                # a non-digit character to separate the seq. no from other identifying chars.""", re.X)


 target_file_fullname = exhibits_temp_dir + '\\' + 'bates_stamp.bat'
 target_file_objname = open(target_file_fullname, 'w')

 dirlist = os.listdir(exhibits_temp_dir)
 for item in dirlist:
     matches_obj = srch_or_match_obj.search(item)
     if matches_obj:
         item_no = str(matches_obj.group(2))

         file_fullname = '"' + os.path.join(exhibits_temp_dir, item) + '"'

         #Cmdline = stamp_prg -PDF "example.pdf" -o "marc_2013-06-12_AM_09-51-54_.pdf" -AT "Exhibit 1, page \p" -P5 -MLR-20 -FN301 -MTB-20
         Cmdline = stamp_prg +' -PDF ' + file_fullname + ' -o ' + file_fullname + ' -AT "Exhibit ' + item_no + ', page \p" -P5 -MLR-20 -FN301 -MTB-20'
         print Cmdline + '\r\n'
         text2write = Cmdline + '\r\n'
         target_file_objname.write(text2write)
         target_file_objname.write('\n\n')

     else:
         print 'Not matched: ' + item

 text2write = 'pause'
 target_file_objname.write(text2write)
 target_file_objname.close
 time.sleep(1)

 RetVal = subprocess.call(target_file_fullname, shell=False)
 print 'Reval = ' + RetVal

 print 'Job done'

【问题讨论】:

【参考方案1】:

显然您的文件在运行时并未关闭。也许是括号(括号):

target_file_objname.close()

【讨论】:

以上是关于Python 子进程无法访问 .bat 文件,因为它被另一个进程使用的主要内容,如果未能解决你的问题,请参考以下文章

在python子进程中将字符串作为参数发送

Python子进程:使用Pipe和通信的困难

python多处理子进程无法访问全局变量

Python 2.7 easy_install - 该进程无法访问该文件,因为它正在被另一个进程错误使用

WMIC + Python 子进程/ (Windows 10)

Python 2.7:调用 subprocess.popen 阻止文件访问