我遇到的问题是,运行程序后,它出错说我的 argv 包含 1 个条目,并且需要将其设置为 0
import subprocess
args = ['/challenge/embryoio_level75']
subprocess.run(args)
Run Code Online (Sandbox Code Playgroud) 在python中,我试图编写一个脚本来编辑文本文件,然后运行使用这些文本文件的可执行文件.它基本上需要1)打开和读/写文本文件,2)使用我刚刚在bash命令中写的文件.这是一个简单的例子:
import subprocess
# write file
a = ['1\n','2\n','3\n','4\n','5th and final line']
f = open('junk01.txt', 'wb')
f.writelines(a)
f.close
# show file
subprocess.call('cat junk01.txt', shell=True)
Run Code Online (Sandbox Code Playgroud)
由于某种原因,该subprocess.call命令没有显示junk01.txt文件的内容.但是,在我运行此代码并键入cat junk01.txtbash后,文件已正确写入.同样,我发现在打开,写入和关闭文本文件然后尝试在可执行文件中使用它之后,该文件尚未写入.有关为什么会这样做以及我可以做些什么来解决它的任何解释?
有没有人知道Python的可移植方式来确定系统的最大命令行长度?我正在编写的程序构建一个命令并将其提供给子进程.对于命令行长度最小的系统,命令可能太长.如果我能检测到这个,那么命令可以被分解以避免超过最大长度,但是我没有找到(便携式)方法来确定最大值.
我需要具有基本上模拟用户使用ssh创建端口转发的功能.所以,本质上应该像如下: -执行ssh -f -N -L 10000:gateway:11000 localhost
-如果有来自该命令的输出,显示给用户,用户泵作为一个响应输入-完成我想出了下面的代码,几乎做什么,我需要:
ssh_proc = Popen(['ssh', '-f', '-N', '-L', '10000:gateway:11000', 'localhost'], stdin=PIPE, stdout=PIPE)
stdoutdata, stderrdata = ssh_proc.communicate()
Run Code Online (Sandbox Code Playgroud)
但问题是它永远不会完成.我看到命令已执行并且已创建转发隧道,但仍然卡住了().我可以用Ctrl + C打破这个并得到这个:
^CTraceback (most recent call last):
File "sshman.py", line 278, in <module>
add(args.remote_host, args.remote_port, args.local_port, args.local_host)
File "sshman.py", line 125, in add
stdoutdata, stderrdata = ssh_proc.communicate()
File "/usr/lib64/python2.7/subprocess.py", line 740, in communicate
return self._communicate(input)
File "/usr/lib64/python2.7/subprocess.py", line 1256, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/usr/lib64/python2.7/subprocess.py", line 1310, in _communicate_with_poll
ready = poller.poll()
KeyboardInterrupt
Run Code Online (Sandbox Code Playgroud)
由于我使用-f和ssh命令,它应该分叉连接.有没有办法在完成后中断communication()或是否有更优雅的解决方案? …
正如问到这个职位,我可以使用Python subprocess.Popen()函数来运行Ruby的代码打印出来的值.
import subprocess
import sys
cmd = ["ruby", "/Users/smcho/Desktop/testit.rb"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
print line,
sys.stdout.flush()
p.wait()
Run Code Online (Sandbox Code Playgroud)
我怎么能用C#做同样的事情?如何打印子进程打印出来的值?
我需要在生成和运行子进程时显示一些进度条或其他东西.我怎么能用python做到这一点?
import subprocess
cmd = ['python','wait.py']
p = subprocess.Popen(cmd, bufsize=1024,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.close()
outputmessage = p.stdout.read() #This will print the standard output from the spawned process
message = p.stderr.read()
Run Code Online (Sandbox Code Playgroud)
我可以使用这段代码生成子进程,但是我需要在每秒传递时打印出一些东西.
我正在尝试获得子流程的结果.但是现在我唯一能得到的结果是日志字符串.我想测试整数结果,但我不知道如何.
buildresult=$(xcodebuild -project $projectfile -nodistribute -activetarget -sdk macosx10.5 "PRODUCT_VERSION_NUM=$PRODUCT_VERSION" 'MACOSX_DEPLOYMENT_TARGET=10.4' 'ARCHS=$(ARCHS_STANDARD_32_BIT)' 'DEMO_PREPROCESSOR_FLAG=_FLUX_DEMO_' "PRODUCT_NAME=$PRODUCTS_ITEM-Demo" 'BASEPRODUCT_NAME=null' -configuration Release build)
Run Code Online (Sandbox Code Playgroud)
$ buildresult包含echo日志,如何测试结果?
谢谢.
在python脚本中,我调用bash脚本如下:
subprocess.Popen(["./scan.sh", dir])
Run Code Online (Sandbox Code Playgroud)
在那个剧本里面,有,
find $1 -name "*013*.txt" > found_files.txt
Run Code Online (Sandbox Code Playgroud)
出于某种原因,python中的dir参数被转换为bash脚本中带引号的版本.在python中打印'dir'会产生用户输入的确切路径:
~/Desktop/Files
Run Code Online (Sandbox Code Playgroud)
但是,找不到了
find: '~/Desktop/Files' no such directory
Run Code Online (Sandbox Code Playgroud)
使用〜/ Desktop/Files手动运行scan.sh可以正常工作.为什么要引用它?...?
我在文件'bin/test'中有一个简单的python脚本:
#!/usr/bin/env python
import argparse
PROGRAM_NAME = "name"
PROGRAM_VERSION = "0.0.1"
PROGRAM_DESCRIPTION = "desc"
parser = argparse.ArgumentParser(prog=PROGRAM_NAME, description=PROGRAM_DESCRIPTION)
parser.add_argument('--version', action='version', version='%(prog)s ' + PROGRAM_VERSION)
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
当我用--versionparam 运行它,或者--help,它打印一切OK:
$ bin/test --version
name 0.0.1
$ bin/test --help
usage: name [-h] [--version]
desc
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
Run Code Online (Sandbox Code Playgroud)
当我使用文件运行时subprocess.check_output,它没有得到任何东西:
>>> subprocess.check_output(["bin/test", "--help"], stderr=subprocess.STDOUT, shell=True)
''
>>> subprocess.check_output(["bin/test", "--version"], stderr=subprocess.STDOUT, shell=True)
''
Run Code Online (Sandbox Code Playgroud)
我正在使用带有Python版本的Ubuntu 11.10: …
我正试图(根据需要)杀死目前正在运行的所有python进程.
我正在使用此命令:
from subprocess import call
call('pkill python', shell=True)
print 'Killed them all!'
Run Code Online (Sandbox Code Playgroud)
但是,当然 - 我的程序也是一个python程序,所以最终,它在调用'call'后没有到达打印行.
我可以做些什么来避免我的程序同时杀死自己,同时杀死所有其他python进程?
谢谢.
subprocess ×10
python ×8
argparse ×1
bash ×1
c# ×1
command-line ×1
find ×1
fork ×1
interprocess ×1
kill ×1
popen ×1
shell ×1
ssh ×1
unix ×1