python脚本与shell脚本的相互调用

python脚本与shell脚本的相互调用

python脚本调用shell脚本:

存在的shell脚本(test_sh.sh):

#!/bin/csh

find . -name 2.txt

在python脚本中调用test_sh.sh脚本

import os
os.system('./test_sh.sh')

当需要传参数到shell脚本中时

shell脚本test_sh.sh:

mv ${
    
    1} ${
    
    2} # 修改传进来文件的名字

python脚本(test.py)

import os
files = os.listdir(r'./')
for f in files:
	os.system('./test_sh.sh' +' '+f + ' '+ '123.txt' )

在linux环境下执行python脚本, python test.py就能改名字了

shell调用python并且传参数

shell脚本(test_sh.sh)内容:

#!/bin/csh
python ./test.py $"hello" $"world" # 传参数,以空格分开

python 脚本(test.py)

import sys
def main(a, b):
	print(a,b)
main(sys.argv[1], sys.argv[2])

在linux环境执行 sh test_sh.sh,就会输出 hello world

猜你喜欢

转载自blog.csdn.net/qq_41841073/article/details/130145389