English 中文(简体)
如何从沙尔方案内的指挥线获取数据?
原标题:How to get data from command line from within a Python program?

我想从字面上操作一条指挥线方案,并取得产出。

我如何获得禁忌所展示的信息,以便我能够用我的文字加以利用?

例如,我从指挥线上打电话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.

问题回答

通常,这门双管齐下:

#!/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

这是一种纯粹的假冒解决办法:

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 ])

在沙里, 您可在<条码>下游上通过带空间、分引和新线的便捷的联科行动指挥。 模块这样,我们就可以把反应文本如上:

  1. 否则就会受到考验。 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;
    
  2. 之后,情况如下:

    python test.py 
    
  3. 印刷:

    this echo command has subquotes, spaces,
    
    
    and newlines!
    

如果这艘船不为你工作,那可能会对金字版或操作系统造成麻烦。 例如,Im在Utub12.10使用2.7.3。





相关问题
Can Django models use MySQL functions?

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 ...

An enterprise scheduler for python (like quartz)

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 ...

How to remove unique, then duplicate dictionaries in a list?

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 ...

What is suggested seed value to use with random.seed()?

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 ...

How can I make the PyDev editor selectively ignore errors?

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 ...

How do I profile `paster serve` s startup time?

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 ...

Pragmatically adding give-aways/freebies to an online store

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 ...

Converting Dictionary to List? [duplicate]

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 ]="...

热门标签