我想从字面上操作一条指挥线方案,并取得产出。
我如何获得禁忌所展示的信息,以便我能够用我的文字加以利用?
例如,我从指挥线上打电话foo file1
,并打印出
Size: 3KB
Name: file1.txt
Other stuff: blah
我怎么能找到像filename = os.system( foo file1 )
我想从字面上操作一条指挥线方案,并取得产出。
我如何获得禁忌所展示的信息,以便我能够用我的文字加以利用?
例如,我从指挥线上打电话foo file1
,并打印出
Size: 3KB
Name: file1.txt
Other stuff: blah
我怎么能找到像filename = os.system( foo file1 )
Use the subprocess module:
import subprocess
command = [ ls , -l ]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.IGNORE)
text = p.stdout.read()
retcode = p.wait()
然后,你可以做你想要的改动<条码>。
The 2nd and 3rd parameters of subprocess.Popen
are optional and can be removed.
利用。 标准图书馆单元。 查阅subprocess.check_output。
>>> subprocess.check_output("echo "foo"", shell=True)
foo
(如果贵工具从未经委托的来源得到投入,则确保不使用<代码>shell=True的论点。)
通常,这门双管齐下:
#!/bin/bash
# vim:ts=4:sw=4
for arg; do
size=$(du -sh "$arg" | awk {print $1} )
date=$(stat -c "%y" "$arg")
cat<<EOF
Size: $size
Name: ${arg##*/}
Date: $date
EOF
done
www.un.org/Depts/DGACM/index_spanish.htm Edit : 如何使用: 开放时间,然后抄送:
cd
wget http://pastie.org/pastes/2900209/download -O info-files.bash
In python2.4 :
import os
import sys
myvar = ("/bin/bash ~/info-files.bash {} ").format(sys.argv[1])
myoutput = os.system(myvar) # myoutput variable contains the whole output from the shell
print myoutput
见。
from subprocess import run
o = run("python q2.py",capture_output=True,text=True)
print(o.stdout)
产出将编码
from subprocess import run
o = run("python q2.py",capture_output=True)
print(o.stdout)
2. 基准
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs)
here output will not be encoded just a tip
这是一种纯粹的假冒解决办法:
import os
import stat
import time
# pick a file you have ...
file_name = ll.py
file_stats = os.stat(file_name)
# create a dictionary to hold file info
file_info = {
fname : file_name,
fsize : file_stats [stat.ST_SIZE],
f_lm : time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_MTIME])),
}
print("""
Size: {} bytes
Name: {}
Time: {}
"""
).format(file_info[ fsize ], file_info[ fname ], file_info[ f_lm ])
在沙里, 您可在<条码>下游条码>上通过带空间、分引和新线的便捷的联科行动指挥。 模块这样,我们就可以把反应文本如上:
否则就会受到考验。 py:
#!/usr/bin/python
import subprocess
command = ( echo "this echo command +
has subquotes, spaces,
" && echo "and newlines!" )
p = subprocess.Popen(command, universal_newlines=True,
shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
text = p.stdout.read()
retcode = p.wait()
print text;
之后,情况如下:
python test.py
印刷:
this echo command has subquotes, spaces,
and newlines!
如果这艘船不为你工作,那可能会对金字版或操作系统造成麻烦。 例如,Im在Utub12.10使用2.7.3。
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...