Page 1 of 1

Can't delete a file in Python script.

Posted: Tue Jan 26, 2010 8:45 am
by l-extreme
Hello my name is Lex and I am new to the beautiful world of EventGost.

I like it very much and tried some things after reading the forum.
I saw that some people had problems doing something with the file name you get from the ÔÇ£DirectoryWatcher".
I make some Python code that used the ÔÇ£DirectoryWatcher" and I hope that it can be used by these people.
I am new to Python, so if thing can be done better, let me now.
I have one error in the script that I can't fix, let me explain:

The code takes a file and print the content on a label printer.
It use the ÔÇ£DirectoryWatcher" to get the file and read the content, take that and send it to an automation control (Brother SDK) to print it to the Brother QL-560 printer.
Everyone who puts an file into the directory checked by the ÔÇ£DirectoryWatcher" gets a label from the Brother printer (witch is a non-network, local printer).
That work fine.
The only thing what not works is to delete the file afterwards.
The error I get is:
Traceback (most recent call last):
Python script "10", line 38, in <module>
os.remove(filename)
WindowsError: [Error 32] Het proces heeft geen toegang tot het bestand omdat het door een ander
proces wordt gebruikt: 'C:\\\\test\\\\Testadres.txt'

Translation of the Dutch Error:
The process can not access the file because it is used by another process:

I tried to delete the file in another Python script but that give the same error.

The content of the file is like this:
NAME=Lex Test
ADRESS=Test street no. 65
POSTCODE=3333 AA
CITY=Utrecht
COUNTRY=Netherlands

The Python code is:

Code: Select all

import os, win32com.client

p = eg.event.payload
a = str(p)
a = a.split("'")
filename = a[1]
print 'Filename = ' + filename #for test

file = open(filename, 'r');
name = ""
adress = ""
postcode = ""
city = ""
country = ""

while 1:
    line = file.readline()
    if line == '':
        break
    i = line.find("=")
    if line[0:i] == "NAME":
        name = line[i + 1:]
    if line[0:i] == "ADRESS":
        adress = line[i + 1:]
    if line[0:i] == "POSTCODE":
        postcode = line[i + 1:]
    if line[0:i] == "CITY":
        city = line[i + 1:]
    if line[0:i] == "COUNTRY":
        country = line[i + 1:]    
    
print name, adress, postcode, city, country # for test
    
file.close

eg.globals.filename = filename

os.remove(filename)

"""
  The code below only works if you have the 
  Brother SDK and USB printer driver installed.

Brother = win32com.client.Dispatch("BrssCom.Document")
bRet = Brother.open("C:\Program Files\Brother bPAC SDK\Templates\Adreslabel.lbl")
print bRet
if bRet == True:
   Brother.SetText(0, name)
   Brother.SetText(1, adress)
   Brother.SetText(2, postcode)
   Brother.SetText(3, city)
   Brother.SetText(4, country)
   Brother.DoPrint(0, "Label, 1")
#bRet = Brother.close()
"""

Re: Can't delete a file in Python script.

Posted: Wed Jan 27, 2010 11:00 am
by Bitmonster
l-extreme wrote:

Code: Select all

file.close
file.close()

Re: Can't delete a file in Python script.

Posted: Wed Jan 27, 2010 2:24 pm
by l-extreme
is it so easy?

Yes it is!!!

Thank you.