【发布时间】:2026-01-06 15:10:02
【问题描述】:
我正在使用 Python3,我想通过 argv 获取 或 >。这是我的代码:
str_args = [ str(x) for x in argv[1:] ] #create sequence
cmd = ''
for i in argv[1:]: #create str
cmd += i + ' '
path = 'cmd.txt'
file=open(path, "a")
file.write(cmd + '\n')
file.close()
在 for 循环中,我连接参数以生成 linux 终端命令并写入 .txt 文件。我在其他地方执行该命令。当我输入 (大于或小于)时,它只会传递到 。示例:
echo 12 > test.txt
它只是得到
回声 12
并且没有通过 > test.txt 我该怎么办 ?它适用于非 示例。例如它很好地传递了这样的命令:
ping 8.8.8.8 -c 2 -s 60
谢谢
【问题讨论】:
标签: python arguments parameter-passing command-line-arguments argv