小编zub*_*ber的帖子

如何在管道中使用`subprocess`命令

我想subprocess.check_output()ps -A | grep 'process_name'.我尝试了各种解决方案,但到目前为止没有任 有人可以指导我怎么做吗?

python linux subprocess pipe

214
推荐指数
6
解决办法
20万
查看次数

Python子进程命令参数

为什么如果我运行subprocess.check_output('ls')一切正在工作,但当我添加参数命令如下:subprocess.check_output('ls -la')我得到错误:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)

如何将命令参数传递给subprocess.check_output()

python linux arguments subprocess

2
推荐指数
1
解决办法
3383
查看次数

为什么循环覆盖我的文件而不是在文本后写?

i = 1 # keep track of file number
directory = '/some/directory/'


for i in range(1, 5170): #number of files in directory
    filename = directory + 'D' + str(i) + '.txt'
    input = open(filename)
    output = open('output.txt', 'w')
    input.readline() #ignore first line
    for g in range(0, 7): #write next seven lines to output.txt
        output.write(input.readline())

    output.write('\n') #add newline to avoid mess
    output.close()
    input.close()
    i = i + 1
Run Code Online (Sandbox Code Playgroud)

我有这个代码,我试图获取一个文件并将其重写为output.txt,但是当我想要附加下一个文件时,我的代码会覆盖已附加的旧文件.结果当代码完成时,我有这样的事情:

dataA[5169]=26
dataB[5169]=0
dataC[5169]=y
dataD[5169]='something'
dataE[5169]=x
data_date[5169]=2012.06.02
Run Code Online (Sandbox Code Playgroud)

而不是从文件0到5169的数据.任何提示如何解决它?

python

1
推荐指数
1
解决办法
5399
查看次数

标签 统计

python ×3

linux ×2

subprocess ×2

arguments ×1

pipe ×1