سؤال

I coded a python script that creates a windows batch file which bates stamps PDF files.

The batch program runs just fine, if open/run it in Windows, e.g., by selecting it and hitting Enger.

But when I try to run subprocess on the batch file, I get the following error message:

 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

I can't figure out how to run subprocess in a way that would prevent the error message from occurring. Any suggestions would be much appreciated, especially references to an explanation of the problem.

Here's the python code:

 # -*- 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'
هل كانت مفيدة؟

المحلول

Apparently you file is not closed when you run it. Maybe the parenthesis (bracket) :

target_file_objname.close()
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top