简体   繁体   English

python中的用户自定义命令行arguments

[英]User-defined command line arguments in python

Encountered a couple of problems with my python program.我的 python 程序遇到了几个问题。 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).基本上,我希望能够将多个图像的路径文件发送到命令提示符(用户定义了多少图像,所以只需将直接文件路径作为 args,如下所示,并不能满足我的要求)。 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.还有更多的图像,所以当然,写出 image_00000004、5、6 等几乎没有效率,完全取决于每次有相同数量的图像,而没有。 (jpivc.jar is the program that opens upon execution of this code, and imports the images and does stuff - that bit works fine). (jpivc.jar 是在执行此代码时打开的程序,它会导入图像并做一些事情——该位工作正常)。 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.除了,你知道,没有打开 jpivc.jar 每次 i 递增,我只是使用上面的内容来展示我想要的东西。 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.我在 python 方面比较缺乏经验,所以请在技术细节上尽可能温和。

My second question is - is there a way of then closing either the command prompt or jpivc.jar?我的第二个问题是 - 有没有办法关闭命令提示符或 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 .我已经尝试了所有我能想到的预定义函数 - os.system("tskill cmd.exe")和随之而来的os.kill() (虽然我使用的是 2.5,所以我对此并不感到惊讶) t 工作), Popen.kill()Popen.terminate() ,甚至尝试使用ctypes编写我自己的 kill function 。 Nothing works - until either cmd.exe or jpivc.jar are closed manually, then everything in the rest of my code works perfectly.没有任何效果 - 直到 cmd.exe 或 jpivc.jar 手动关闭,然后我的代码的 rest 中的所有内容都可以正常工作。 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;我认为 Python 会停止,直到命令行关闭 - 当我想从 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.如果需要,可以从我的代码中提供更多摘录 - 我在 Windows XP Professional 上使用 Python 2.5.4。 Don't ask me to provide anything from jpivc.jar - I won't even pretend to understand the slightest bit of Java.不要让我提供来自 jpivc.jar 的任何东西——我什至不会假装理解一点 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.将您的文件从 script.py 重命名为 script.pyw 以避免打开命令提示符。

jpivc.jar will remain open until you close it manually or programmer changes its code to quit after it completes processing all files. jpivc.jar 将保持打开状态,直到您手动关闭它或程序员在完成处理所有文件后更改其代码以退出。

[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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM