1 Replies - 2090 Views - Last Post: 12 December 2011 - 03:04 PM Rate Topic: -----

#1 dr_worm   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 144
  • Joined: 11-September 06

Python script causes error when run outside of IDLE

Posted 12 December 2011 - 02:32 PM

Hey all, it's been a while since I've been on here :)

I'm using Python 2.4 and writing a script to automate a process given a certain event; specifically, I want to monitor the CD drive on a Windows box and run a command-line virus scan whenever a CD is inserted. The program is McAfee's VirusScan Enterprise, which has a command-line tool called csscan.exe. This is my code (comments added for clarity):

import os

scanned = 0 #boolean flag, determines whether to run a scan or wait for the CD to be ejected

while 1: #once started, should always run, in background
    if not(scanned):
        try:
            if os.access('E:\\', os.F_OK): #once you can access the E: drive, scan it
                print 'scanning' #debugging message
                scanned = 1 #only scan the CD once, then move into the 'holding pattern'
                rtn = os.system('csscan.exe -Silent -Sub -Secure -MAnalyze -PAnalyze -Analyze -MultiExt -AllOle -Archive -Mime -AllApps -PUP -Clean -Secondary Delete -SecondaryApp Delete E:\\') #the folder containing csscan.exe is in my path, and all -Options are for csscan.exe
                print 'rtn is', rtn #debugging message
                print 'scan complete' #debugging message
        except: #trying (and failing) to handle the error dialog that pops up
            continue
    else:
        if not(os.access('E:\\', os.F_OK)): #once the CD is ejected, reset the flag to scan again later
            print 'resetting scan check' #debugging message
            scanned = 0



When I run the script from IDLE (the only IDE that comes installed on this box, and no, I can't install another), the script runs as expected. When I double-click the script to run it outside of IDLE and there is no CD in the drive, Windows throws this error (read: this error dialog pops up in a box):

python.exe - No Disk
There is no disk in the drive. Please insert a disk into drive E:.
<Cancel> <Try Again> <Continue>



If I insert a CD and click "Try Again," the script operates as expected, but as soon as I take the CD out, the error dialog pops up again.

I've done almost all of my coding based on the information I could find in the included documentation, as my internet access has been restricted, so any help on how to wait/listen for the CD drive or how better to run this script in the background than to stick it in the Startup folder would be much appreciated!

Thanks in advance for your help, and I look forward to getting involved in the community here again!

Cheers,

dr_worm

Is This A Good Question/Topic? 0
  • +

Replies To: Python script causes error when run outside of IDLE

#2 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Python script causes error when run outside of IDLE

Posted 12 December 2011 - 03:04 PM

Simple solution: run a command window at startup that executes your python script.

Actual solution: Rewrite your python code and leverage the ctypes library to issue IOCTL_STORAGE_CHECK_VERIFY commands using the Kernel32 function DeviceIoControl.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1