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

时间:2014-04-07 15:41:15

标签: python-2.7 batch-file pdf-generation subprocess

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

批处理程序运行得很好,如果在Windows中打开/运行它,例如选择它并按下Enger。

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

 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 个答案:

答案 0 :(得分:1)

显然,运行它时文件未关闭。也许是括号(括号):

target_file_objname.close()