【发布时间】:2015-03-01 00:25:04
【问题描述】:
目前我正在使用两个单独的 shell 脚本来完成工作。
1) 列出当前目录并将其保存为 .html 文件(首先仅列出根目录,然后完整列出)
tree -L 1 -dH ./ >> /Volumes/BD/BD-V1.html && tree -H ./ >> /Volumes/BD/BD-V1.html
2) 使用 sed 删除不需要的行(我在 mac 上)
sed -i '' '/by Francesc Rocher/d' /Volumes/BD/BD-V1.html && sed -i '' '/by Steve Baker/d' /Volumes/BD/BD-V1.html && sed -i '' '/by Florian Sesser/d' /Volumes/BD/BD-V1.html
现在我想将它们作为一个脚本与用户输入的文件路径结合起来。我试图用python做,但没有成功
import subprocess
subprocess.call(["tree", "-d", "-L", "1"])
上面可以列出目录但我无法保存输出(我必须在 python 内部执行此操作),我尝试了类似的操作但没有成功。
file = open('out.txt', 'w')
import subprocess
variation_string = subprocess.call(["tree", "-d", "-L", "1"])
file.write(variation_string)
file.close()
我也不确定如何实现 sed :(
编辑:我是初学者
【问题讨论】:
-
由于您显然是 shell 命令和 Python 的初学者,我建议您现在不要同时使用这两种命令。继续使用 shell,花点时间编写一个 shell 脚本来做你想做的事。 Shell 脚本可以很好地接受用户输入,例如通过参数之类的命令。
-
@Jan-PhilipGehrcke 谢谢,现在我都在 shell 脚本中完成了。