Python问题:如何在shell中执行一个模块,这个模块需要传入参数?

bluishsky 2004-04-14 11:51:34
我有一个模块叫testargv.py:

#!/usr/bin/env python
# -*- coding: cp936 -*-


if __name__ == '__main__':
if len(argv) < 3:
print 'argv[0]为:',argv[0]
print '参数不足够,须再输入argv[1]和argv[2]'
print
else:
try:
print 'argv[1]为:',argv[1]
print 'argv[2]为:',argv[2]
except ValueError, e:
print 'error: ' + str(e)
print 'run with no args for parameter explanations'

但是我不知道如何在Python Shell命令行中调用该模块testargv.py来测试,菜鸟问题,忘不吝赐教!
...全文
904 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
limodou 2004-04-16
  • 打赏
  • 举报
回复
是不是应该加两个'\\'
os.system('D:\\Program Files\\Python testargv arg01 arg02')
PandaEyes 2004-04-16
  • 打赏
  • 举报
回复
在shell中要定好路径
bluishsky 2004-04-16
  • 打赏
  • 举报
回复
我的是windows2000Server,就是没停下来
duracell 2004-04-15
  • 打赏
  • 举报
回复
import os

os.system('c:\路径\python testMain arg1 arg2')
duracell 2004-04-15
  • 打赏
  • 举报
回复
>>> os.system('D:\Program Files\Python testargv arg01 arg02')

怎么我试的就是停下来的?
linux 和 xp下面都试过没问题啊?
bluishsky 2004-04-15
  • 打赏
  • 举报
回复
谢谢limodou(),您说的正确。

http://expert.csdn.net/Expert/topic/2961/2961426.xml?temp=.633526

也有相应的解决结果。
bluishsky 2004-04-15
  • 打赏
  • 举报
回复
thank duracell() !!!!

这个语句正是我想要的,但是现在出了一个新问题,我没法让cmd窗口停下来等我观察,它很快就消失了。

我把我的testargv.py代码改为如下情况,仍然不能让它停下来:
#!/usr/bin/env python
# -*- coding: cp936 -*-

import sys

if __name__ == '__main__':
if len(sys.argv) < 3:
print 'argv[0]为:',sys.argv[0]
print '参数不足够,须再输入argv[1]和argv[2]'
print
else:
try:
print 'argv[1]为:',sys.argv[1]
print 'argv[2]为:',sys.argv[2]
except ValueError, e:
print 'error: ' + str(e)
print 'run with no args for parameter explanations'

raw_input("press any key to exit...%d"%1);

cmd窗口居然不等我输入就被销毁了
我在idle里的执行情况如下:
>>> import os
>>> os.system('D:\Program Files\Python testargv arg01 arg02')
1
>>>

os.system('D:\Program Files\Python testargv arg01 arg02')很快就执行并返回。
我该如何让cmd窗口停下来等我做相应的交互后再返回?
xiaonian_3654 2004-04-14
  • 打赏
  • 举报
回复
直接在命令行上输入阿
bluishsky 2004-04-14
  • 打赏
  • 举报
回复
我的testargv.py存在Python的根目录下,在Python Shell中运行如下显示:
>>> testargv.py

Traceback (most recent call last):
File "<pyshell#31>", line 1, in -toplevel-
testargv.py
NameError: name 'testargv' is not defined
>>> import testargv
>>> testargv
<module 'testargv' from 'D:\PROGRA~1\Python\testargv.py'>
>>> testargv.py

Traceback (most recent call last):
File "<pyshell#34>", line 1, in -toplevel-
testargv.py
AttributeError: 'module' object has no attribute 'py'
>>> testargv myarg1 myarg2
SyntaxError: invalid syntax
>>> testargv 'myarg1' 'myarg2'
SyntaxError: invalid syntax
>>> testargv --'myarg1' --'myarg2'

Traceback (most recent call last):
File "<pyshell#37>", line 1, in -toplevel-
testargv --'myarg1' --'myarg2'
TypeError: bad operand type for unary -
>>>

仍然没找到调用的办法~~~~~~~~~~
xiaonian_3654 2004-04-14
  • 打赏
  • 举报
回复
testargv.py 参数1 参数2
limodou 2004-04-14
  • 打赏
  • 举报
回复
albertlee 说的是在linux下的,不是windows下的。
在命令行下运行可以是:
python testargv argv1 argv2

bluishsky 2004-04-14
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2961/2961426.xml?temp=.633526
bluishsky 2004-04-14
  • 打赏
  • 举报
回复
问题在dos下已经解决

对于albertlee(说你傻,你就流鼻涕) 的解决办法我还没有测试成功。

希望继续给出解释。
albertlee 2004-04-14
  • 打赏
  • 举报
回复
chmod +x yourfile.py

bluishsky 2004-04-14
  • 打赏
  • 举报
回复
xiaonian_3654(你猜猜) 老大,忘了给你说了,我在windows上用的IDLE(Python GUI),你说的在我这好像完全行不通,不信你试试
xiaonian_3654 2004-04-14
  • 打赏
  • 举报
回复
运行cmd,
c:\python\testargv.py
bluishsky 2004-04-14
  • 打赏
  • 举报
回复
我用的是idle,你看到的,上面是我在命令行输入及出错结果,要不您帮我试试?把命令行输入的语句像我这样拷贝下来,显示给我看?谢谢

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧