簡體   English   中英

為UNIX命令行編寫python腳本

[英]write python script for UNIX command lines

嘿,我下周有一個使用編譯器的基准測試,教授希望我們實際上是在命令行中編寫腳本。 我是python新手,在UNIX上寫腳本。 您如何在Python上編寫腳本? 我需要教程或任何建議

謝謝

我們必須編寫這些步驟

對於每個實驗,請按照以下步驟操作:

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. 

subprocess.Popen,os.listdir,os.chdir和time.time()應該會有所幫助。 這些都是python命令。 例如,您可以使用“ import subprocess”獲得子流程模塊,並使用“ import os”獲得os模塊。

要創建Python腳本,只需使用您喜歡的文本編輯器在* ix上編輯文本文件(此示例中為“ the-script”),其內容如下:

#!/usr/bin/python3
# or you can use python 2 with /usr/bin/python

print('hello world')

...並使其可執行:

chmod 755 the-script

...然后您可以像這樣運行它:

./the-script

高溫超導

要在外殼中調用某些內容,請使用以下命令:

>>> import os
>>> os.system('echo hello')
hello
0
>>> 

要么:

>>> import subprocess
>>> subprocess.call(['echo', 'hello'])
hello
0
>>> 

如果要使用ls命令,請使用以下代碼:

>>> import os
>>> x = os.popen('ls ~/Desktop').read().split()
>>> x
['...
']

我認為這就是您的意思,但是如果您可以在問題中添加更多詳細信息,那就太好了。

使用Popen構造函數在python中調用shell命令。 看這個答案

另外,這是另一個代碼:

   from subprocess import Popen, PIPE

    output = Popen(['ls -l'], stdout=PIPE, stderr=PIPE, shell=True)
    (out,err) = output.communicate()

out包含來自ls -l的stdout,而err包含來自ls -l的stderr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM