我正在尝试制作一个在文件中搜索单词的python脚本.如果我通过txt它只会查看扩展名为.txt的文件,但我想传递*参数来搜索每个文件.
if sys.argv[4] == "*"
Run Code Online (Sandbox Code Playgroud)
不要工作,如果我尝试
print sys.argv[4]
Run Code Online (Sandbox Code Playgroud)
它打印脚本的名称
find.py
Run Code Online (Sandbox Code Playgroud)
但不一样
print sys.argv[0]
Run Code Online (Sandbox Code Playgroud)
因为它会回来
./find.py
Run Code Online (Sandbox Code Playgroud)
所以,有人已经遇到了这个问题,当然,解决了这个问题?
你的shell也有意义*.调用脚本时需要转义它以防止shell扩展它:
python find.py \*
Run Code Online (Sandbox Code Playgroud)
sys.argv[0]是用于运行脚本的确切名称.这可以是相对路径(./find.py,../bin/find.py)或绝对路径,具体取决于它的调用方式.使用os.path.abspath()正常化它.