无法传递命令行参数 [英] can't pass command-line arguments
问题描述
我还是新手。我不能让它作为一个脚本工作。如果我只是
手动插入sys.argv [1]和sys.argv [2]的值它可以工作
罚款,但我不能传递变量从命令行。我做了什么?b $ b做错了什么?在windows xp,python 2.4.3
谢谢
导入os
导入fnmatch
import sys
def all_files(root,patterns =''*'',single_level = False,
yield_folders = False):
#从分号分隔的字符串扩展模式到列表
patterns = patterns.split('';'')
路径,子目录,文件的
在os.walk(root):
如果yield_folders:
files.extend(subdirs)
files.sort()
用于文件中的名称:
用于模式中的模式:
如果fnmatch.fnmatch(名称,模式):
yield os .path.join(路径,名称)
休息
如果single_level:
休息
for all_files中的路径(sysargv [1],sysargv [2]):
打印路径
ps - 原始脚本来自优秀的Python Cookbook,但是
显然我试图通过传递参数来打破它:)
I''m still new at this. I can''t get this to work as a script. If I just
manually insert the values for sys.argv[1] and sys.argv[2] it works
fine, but I can''t pass the variables from the command line. What am I
doing wrong? On windows xp, python 2.4.3
Thank you
import os
import fnmatch
import sys
def all_files(root, patterns=''*'', single_level=False,
yield_folders=False):
# Expand patterns from semicolon-separated string to list
patterns = patterns.split('';'')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
for path in all_files(sysargv[1], sysargv[2]):
print path
ps - The original script is from the excellent Python Cookbook, but
obviously I''m breaking it by trying to pass arguments to it :)
推荐答案
Em Dom,2006-04-09?* s 19:41 -0700,BartlebyScrivener escreveu:
Em Dom, 2006-04-09 ?*s 19:41 -0700, BartlebyScrivener escreveu:
for all_files中的路径(sysargv [1], sysargv [2]):
for path in all_files(sysargv[1], sysargv[2]):
而不是sysargv,使用sys.argv。
-
Felipe。
Instead of sysargv, use sys.argv.
--
Felipe.
Duh! Headsmack。
谢谢。但是,我发现了别的东西。如果我将脚本命名为
findmyfiles.py并在命令行中运行它,而在目录
存储它(在Windows上),我必须将其运行为:
findmyfiles.py d:/ notes notes *。*
我以前只需输入脚本名就可以运行脚本,
即使没有.py扩展名,但
findmyfiles d:/ notes notes *。*不起作用
谢谢你,Felipe
Duh! Headsmack.
Thanks. But also, I discovered something else. If I name the script
findmyfiles.py and run it from the command line while in the directory
where it is stored (on windows), I must run it as:
findmyfiles.py d:/notes notes*.*
I was used to being able to run scripts by just typing the script name,
even without the .py extension, but
findmyfiles d:/notes notes*.* does not work
Thank you, Felipe
文章< 11 ******************* ***@i40g2000cwc.googlegroups .com> ;,
" BartlebyScrivener" < RP ******* @ gmail.com>写道:
In article <11**********************@i40g2000cwc.googlegroups .com>,
"BartlebyScrivener" <rp*******@gmail.com> wrote:
我习惯只能输入脚本名称来运行脚本,即使没有.py扩展名,
findmyfiles d:/ notes notes *。*不起作用
I was used to being able to run scripts by just typing the script name,
even without the .py extension, but
findmyfiles d:/notes notes*.* does not work
构建Windows的MS-DOS基础只支持一个小的
可执行的扩展数量文件(.COM,.EXE和.BAT),
没有规定任何扩展名。
The MS-DOS foundation on which Windows is built only supports a small
number of extensions for "executable" files (.COM, .EXE and .BAT), with
no provision for any extensions to these.
这篇关于无法传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!