开发者

User-defined command line arguments in python

Encountered a couple of problems with my python program. Basically, I want to be able to send the path files of multiple images to the command prompt (the user defines how many images, so just putting the direct file paths as args, as below, doesn't do what I want). My code currently looks like this:

os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_00000001.jpg image_00000002.jpg image_00000003.jpg")

There are many more images, so of course, writing out image_00000004, 5, 6, etc, is hardly efficient and depends entirely on there being the same number of images each time, which there isn't. (jpivc.jar is the program that opens upon execution of this code, and imports the images and does stuff - that bit works fine). Ideally, the code would be something like:

for i in range(1, NumberOfImages):
    os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_0000000" + i + ".jpg")

Except, you know, without opening jpivc.jar each time i is incremented, I'm just using the above to show the kind of thing I want. Unfortunately this doesn't work, as it only sends the first part in " " to the command line, if it doesn't give an error message - my first question is, is there any way of making this do what I want it to do? I'm relatively inexperienced at python, so please be as gentle as possible with the technical details.

My second question is - is there a way of then closing either the command prompt or jpivc.jar? I've tried all the predefined functions I can think of - os.system("tskill cmd.exe") and variatons thereupon, os.kill() (although I'm using 2.5, so I'm not surprised this doesn't work), Popen.kill() and Popen.terminate(), and even tried writing my own kill function using ctypes. Nothing works - until either cmd.exe or jpivc.jar are closed manually, then everything in the rest of my 开发者_运维知识库code works perfectly. I think Python halts until the command line is closed - which isn't helpful when I want to then close the command line from Python! To sum up; why does it halt like that, and how can I fix it?

More extracts from my code can be provided if needed - I'm using Python 2.5.4 on Windows XP Professional. Don't ask me to provide anything from jpivc.jar - I won't even pretend to understand the slightest bit of Java.

Thanks in advance,

Dave


I believe you are looking for something like this

fileNames=""
for i in range(1, NumberOfImages):
    fileNames += "image_0000000%d.jpg "%i

os.system( "java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar %s"%fileNames )

Rename your file from script.py to script.pyw to avoid the opening of command prompt.

jpivc.jar will remain open until you close it manually or programmer changes its code to quit after it completes processing all files.

[EDIT]

I found this for starting and killing a process

pidId = os.spawnl(os.P_NOWAIT, "\\windows\\notepad.exe") 
import win32api
win32api.TerminateProcess(pidId ,0)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜