2008年01月22日 星期二 12:27
Hi All,
ÏëÓÃPythonÀ´µ÷ÓÃϵͳÃüÁÓöµ½ÁËÎÊÌâ¡£
ÀýÈçÏëµ÷ÓÃa.exe£¬²¢¸úһЩ²ÎÊý£¬ÃüÁî¸ñʽΪ£º a.exe -a -b -c
ÓÉÓÚÓû§µ±Ç°¹¤×÷Ŀ¼²»È·¶¨£¬ÓÚÊǾö¶¨¿¼ÂÇʹÓþø¶Ô·¾¶
c:\a.exe -a -b -c
ÓÉÓÚa.exeµÄ·¾¶²»È·¶¨£¬ÓпÉÄÜ´æÔÚ
c:\document and settings\usera\a.exe -a -b -cµÄÇé¿ö£¬¿Õ¸ñµ¼ÖÂÃüÁîÎÞ·¨Õý³£Ö´ÐС£
ÓÃos.system("c:\document and settings\usera\a.exe -a -b -c")¾Í»á³öÏÖ´íÎó¡£
ÎÒʹÓà "c:\document and settings\usera\a.exe" -a -b -c ÔÚcommand lineÀïÃæÊÇÕý³£Ö´Ðеģ¬
µ«ÊÇʹÓÃos.system()µÄ»°£¬Ìáʾ´íÎó¡£²»ÖªµÀΪʲô¡£
ÓÚÊǾö¶¨¿¼ÂÇʹÓÃosÖеÄexecvº¯ÊýÀ´Ö´ÐÐÕâ¸öÃüÁµ«ÊÇ·¢ÏÖËùÓеÄexec*º¯Êý¶¼Ìáµ½»áreplace current process¡£
ÓÐûÓк¯ÊýÄÜ´ïµ½execvµÄЧ¹û£¬µ«ÊDz»replace current processÄØ£¿
Ê®·Ö¸Ðл¡£
--
Best Regard,
Tang, Jiyu (Joey)
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20080122/8a86bb45/attachment.html
2008年01月22日 星期二 12:49
os.popen
def cmn_exec_cmd_and_get_list( cmd, split_symbol = '\n' ):
""" execute a external command and split output into list, then return
the list
"""
out = os.popen( cmd, 'r')
out_txt = out.read()
ret = out.close()
if ret:
raise RuntimeError, '%s failed with exit code %d' % (cmd, ret)
list = out_txt.split( split_symbol )
return list
windows 下的路径有时候有 8.3 的历史遗留问题,换成 ~1 这样就好了。
> Hi All,
> 想用Python来调用系统命令,遇到了问题。
> 例如想调用a.exe,并跟一些参数,命令格式为: a.exe -a -b -c
> 由于用户当前工作目录不确定,于是决定考虑使用绝对路径
> c:\a.exe -a -b -c
> 由于a.exe的路径不确定,有可能存在
> c:\document and settings\usera\a.exe -a -b -c的情况,空格导致命令无法
> 正常执行。
> 用os.system("c:\document and settings\usera\a.exe -a -b -c")就会出现错误。
> 我使用 "c:\document and settings\usera\a.exe" -a -b -c 在command line
> 里面是正常执行的,但是使用os.system()的话,提示错误。不知道为什么。
> 于是决定考虑使用os中的execv函数来执行这个命令,但是发现所有的exec*函数
> 都提到会replace current process。
> 有没有函数能达到execv的效果,但是不replace current process呢?
> 十分感谢。
>
> --
> Best Regard,
> Tang, Jiyu (Joey)
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2008年01月22日 星期二 12:53
python-chinese£¬ÄãºÃ os.system(r"c:\document and settings\usera\a.exe -a -b -c") nmweizi£¬nmweizi在163.com 2008-01-22 ----- Original Message ----- From: Joey To: python-chinese Sent: 2008-01-22, 12:27:17 Subject: [python-chinese] osÖеÄexecl, execle, execlpµÈº¯ÊýÓÃÀ´Ö´ÐÐϵͳÃüÁΪʲô»áreplace current processÄØ£¿ Hi All, ÏëÓÃPythonÀ´µ÷ÓÃϵͳÃüÁÓöµ½ÁËÎÊÌâ¡£ ÀýÈçÏëµ÷ÓÃa.exe£¬²¢¸úһЩ²ÎÊý£¬ÃüÁî¸ñʽΪ£º a.exe -a -b -c ÓÉÓÚÓû§µ±Ç°¹¤×÷Ŀ¼²»È·¶¨£¬ÓÚÊǾö¶¨¿¼ÂÇʹÓþø¶Ô·¾¶ c:\a.exe -a -b -c ÓÉÓÚa.exeµÄ·¾¶²»È·¶¨£¬ÓпÉÄÜ´æÔÚ c:\document and settings\usera\a.exe -a -b -cµÄÇé¿ö£¬¿Õ¸ñµ¼ÖÂÃüÁîÎÞ·¨Õý³£Ö´ÐС£ ÓÃos.system("c:\document and settings\usera\a.exe -a -b -c")¾Í»á³öÏÖ´íÎó¡£ ÎÒʹÓà "c:\document and settings\usera\a.exe" -a -b -c ÔÚcommand lineÀïÃæÊÇÕý³£Ö´Ðе쬵«ÊÇʹÓÃos.system()µÄ»°£¬Ìáʾ´íÎó¡£²»ÖªµÀΪʲô¡£ ÓÚÊǾö¶¨¿¼ÂÇʹÓÃosÖеÄexecvº¯ÊýÀ´Ö´ÐÐÕâ¸öÃüÁµ«ÊÇ·¢ÏÖËùÓеÄexec*º¯Êý¶¼Ìáµ½»áreplace current process¡£ ÓÐûÓк¯ÊýÄÜ´ïµ½execvµÄЧ¹û£¬µ«ÊDz»replace current processÄØ£¿ Ê®·Ö¸Ðл¡£ -- Best Regard, Tang, Jiyu (Joey) -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20080122/dec24033/attachment.html
2008年01月22日 星期二 12:59
exec 就是替换当前进程. 你需要的是 popen 或 system 你的错误在于引号理用了 \ 却没转义,也没保留引号 你需要用 os.system(r"'c:\document and settings\usera\a.exe' -a -b -c") 注意加的 r 和 里边的单引号 Linux 下 os.popen 可以用参数数组,而 Windows 下必须用字符串, 所以你还是得处理空格 2008/1/22 Joey <tang.jiyu at gmail.com>: > Hi All, > > 想用Python来调用系统命令,遇到了问题。 > 例如想调用a.exe,并跟一些参数,命令格式为: a.exe -a -b -c > 由于用户当前工作目录不确定,于是决定考虑使用绝对路径
2008年01月22日 星期二 21:53
在cmd里有空格时用多重引号,如:
os.system('""c:\document and settings\usera\a.exe" "-a "-b" "-c""')
注意两头有3个引号,一个单引号,两个双引号
单引号是python的需要,表示字符串,两个双引号是cmd的需要
在cmd里,os.system(r"c:\document and settings\usera\a.exe -a -b
-c")这个肯定不行,因为cmd无法处理其中的空格。
这个问题也曾困扰我很久,我记得好像是在微软的网站上找到的答案。
在08-1-22,Joey <tang.jiyu在gmail.com> 写道:
>
> Hi All,
>
> 想用Python来调用系统命令,遇到了问题。
> 例如想调用a.exe,并跟一些参数,命令格式为: a.exe -a -b -c
> 由于用户当前工作目录不确定,于是决定考虑使用绝对路径
> c:\a.exe -a -b -c
> 由于a.exe的路径不确定,有可能存在
> c:\document and settings\usera\a.exe -a -b -c的情况,空格导致命令无法正常执行。
> 用os.system("c:\document and settings\usera\a.exe -a -b -c")就会出现错误。
> 我使用 "c:\document and settings\usera\a.exe" -a -b -c 在command line里面是正常执行的,
> 但是使用os.system()的话,提示错误。不知道为什么。
>
> 于是决定考虑使用os中的execv函数来执行这个命令,但是发现所有的exec*函数都提到会replace current process。
>
> 有没有函数能达到execv的效果,但是不replace current process呢?
>
> 十分感谢。
>
> --
> Best Regard,
> Tang, Jiyu (Joey)
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20080122/6cd95b04/attachment.html
Zeuux © 2025
京ICP备05028076号