首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为UNIX命令行编写python脚本

为UNIX命令行编写python脚本
EN

Stack Overflow用户
提问于 2014-04-24 09:21:34
回答 3查看 4.2K关注 0票数 0

嘿,我下周有个作业,用编译器做基准测试,教授让我们写一个脚本,实际上是命令行。我是python新手,也是在UNIX上编写脚本的新手。如何在Python上编写脚本?我需要教程或任何建议

谢谢

我们必须编写这些步骤

对于每个实验,请执行以下步骤:

代码语言:javascript
复制
For each benchmark directory, change to that directory and compile the code with one of the optimization levels. For example:

cd adpcm
gcc -O0 -o adpcm-O0 adpcm.c

Time the runtime of the executable:

time ./adpcm-O0
Record the “real” time displayed. You might take an average of 3-5 runs to get a stable result.

Use the performance measurement tools

On the Pis:

run rpistat on the executable
e.g. rpistat ./adpcm-O0
that generates a textfile rpistat.txt in the same directory. Record the Cycles and Instructions (use the value in [brackets] for the instruction count).

On the lab workstations:

run perf on the executable
e.g. perf stat ./adpcm-O0
that prints to stdout (you can redirect if you wish). Record the Cycles and Instructions.

Repeat this procedure for all 12 benchmarks and all 4 optimization levels on both machines.
For the fastest version of each benchmark (use lowest cycle count in the case of a tie), profile the application:

gcc -pg -O2 -o adpcm-prof adpcm.c
./adpcm-prof  (This is needed to profile the executable, but you don’t need to record the runtime)
gprof ./adpcm-prof | less
Record the function for which the most execution time is spent. 
EN

回答 3

Stack Overflow用户

发布于 2014-04-24 09:34:46

subprocess.Popen、os.listdir、os.chdir和time.time()应该会有所帮助。这些都是python命令。例如,你可以用"import subprocess“得到子进程模块,用"import os”得到os模块。

要创建一个Python脚本,只需在*ix上使用您喜欢的文本编辑器编辑一个文本文件(本例中名为"the- script“),内容如下:

代码语言:javascript
复制
#!/usr/bin/python3
# or you can use python 2 with /usr/bin/python

print('hello world')

...and使其成为可执行文件:

代码语言:javascript
复制
chmod 755 the-script

...then您可以像这样运行它:

代码语言:javascript
复制
./the-script

HTH

票数 3
EN

Stack Overflow用户

发布于 2014-04-24 09:24:18

要在shell中调用某些内容,请使用以下命令:

代码语言:javascript
复制
>>> import os
>>> os.system('echo hello')
hello
0
>>> 

或:

代码语言:javascript
复制
>>> import subprocess
>>> subprocess.call(['echo', 'hello'])
hello
0
>>> 

如果要使用诸如ls之类的命令,请使用以下代码:

代码语言:javascript
复制
>>> import os
>>> x = os.popen('ls ~/Desktop').read().split()
>>> x
['...
']

我想这就是你的意思,但如果你能在你的问题中添加更多的细节,那就太好了。

票数 0
EN

Stack Overflow用户

发布于 2014-04-24 09:25:43

使用Popen构造函数在python中调用外壳命令。看看这个answer

另外,下面是另一段代码:

代码语言:javascript
复制
   from subprocess import Popen, PIPE
    
    output = Popen(['ls -l'], stdout=PIPE, stderr=PIPE, shell=True)
    (out,err) = output.communicate()

out包含来自ls -l的标准输出,err包含来自ls -l的标准错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23258111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档