如何在python代码中使用args运行python

时间:2014-04-14 15:13:59

标签: python python-2.7

我试图用Python编写一个扫描文件然后将其插入命令的程序 当我尝试运行" python submit.py path / arg"我收到一个错误。 我尝试了很多命令但没有成功(os.system,os.Popen,subprocess.call,exec) 提前致谢

这是我的计划:

#!/usr/bin/python
import os
import glob
import subprocess
import commands
import time
import threading

exe = []
os.chdir("/home/malwares")
exe = glob.glob('*.exe')
exe1 = ''.join(exe)
exe1 = exe1.replace("']", "")
exe1 = exe1.replace("['", "")
os.chdir("/home/utils/")

//here i tried to run python script with args
subprocess.call(["python", "submit.py", "/home/malwares",exe1])

1 个答案:

答案 0 :(得分:1)

我对你str.replace的电话感到困惑。为什么你的文件名包含[']?但是,好吧,公平地说,让我们至少做到这一点......

os.chdir('/home/malwares')
exe = glob.glob('*.exe') # no need to initialize this first.
exe_str = ''.join(exe) # name it a little more descriptively than exe1
trans_table = str.maketrans('', '', "[']")
exe_str = exe_str.translate(trans_table)
# this is probably faster than
# ''.join( [filename.translate(trans_table) for filename in glob.glob('*.exe')] )
# but you may want to profile it

subprocess.call( ['python', 'submit.py', '/home/malwares', exe_str] )
# is this REALLY what you want??? This will do:
# >> python submit.py /home/malwares FILEONE.exeFILETWO.exeFILETHREE.exe
# maybe you should have used ' '.join(exe) instead?