运行 bash while 循环 Python
Run bash while loop in Python
我正在尝试 运行 bash
while 在 Python3.6
脚本中循环。到目前为止我尝试过的是:
subprocess.run(args=['while [ <condition> ]; do <command> done;'])
我收到以下错误:
FileNotFoundError: [Errno 2] No such file or directory
有没有办法在 Python
中 运行 这样的 while 循环?
让您感到困惑的部分是将参数作为列表提供。来自 documentation:
If the cmd argument to popen2 functions is a string, the command is executed through /bin/sh. If it is a list, the command is directly executed.
这似乎是你想要的:
subprocess.run('i=0; while [ $i -lt 3 ]; do i=`expr $i + 1`; echo $i; done', shell=True)
请注意,它被指定为字符串而不是列表。
运行 bash for loop
in Python 3.x
非常像 运行ning while
循环。
#! /bin/bash
for i in */;
do
zip -r "${i%/}.zip" "$i";
done
这将遍历路径并压缩所有目录。 运行 bash 上面的 Python 脚本:
import subprocess
subprocess.run('for i in */; do zip -r "${i%/}.zip" "$i"; done', shell=True)
我正在尝试 运行 bash
while 在 Python3.6
脚本中循环。到目前为止我尝试过的是:
subprocess.run(args=['while [ <condition> ]; do <command> done;'])
我收到以下错误:
FileNotFoundError: [Errno 2] No such file or directory
有没有办法在 Python
中 运行 这样的 while 循环?
让您感到困惑的部分是将参数作为列表提供。来自 documentation:
If the cmd argument to popen2 functions is a string, the command is executed through /bin/sh. If it is a list, the command is directly executed.
这似乎是你想要的:
subprocess.run('i=0; while [ $i -lt 3 ]; do i=`expr $i + 1`; echo $i; done', shell=True)
请注意,它被指定为字符串而不是列表。
运行 bash for loop
in Python 3.x
非常像 运行ning while
循环。
#! /bin/bash
for i in */;
do
zip -r "${i%/}.zip" "$i";
done
这将遍历路径并压缩所有目录。 运行 bash 上面的 Python 脚本:
import subprocess
subprocess.run('for i in */; do zip -r "${i%/}.zip" "$i"; done', shell=True)