命令模块问题
你好,我正在尝试通过导入命令模块在 python 中执行 bash 命令。我想我之前在这里问过同样的问题。然而这一次不起作用。 脚本如下:
#!/usr/bin/python
import os,sys
import commands
import glob
path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')
for bamfile in bamfiles:
fullpath = os.path.join(path,bamfile)
txtfile = commands.getoutput('/share/bin/samtools/samtools ' + 'view '+ fullpath)
line=txtfile.readlines()
print line
这个 samtools 视图将生成(我认为).txt 文件
我收到错误:
Traceback (most recent call last):
File "./try.py", line 12, in ?
txtfile = commands.getoutput('/share/bin/samtools/samtools ' + 'view '+ fullpath)
File "/usr/lib64/python2.4/commands.py", line 44, in getoutput
return getstatusoutput(cmd)[1]
File "/usr/lib64/python2.4/commands.py", line 54, in getstatusoutput
text = pipe.read()
SystemError: Objects/stringobject.c:3518: bad argument to internal function
似乎是commands.getoutput 的问题
谢谢
Hi I'm trying to execute bash command in python by importing commands module.I think I ask the same question here before. However this time it doesn't work.
The script is as below:
#!/usr/bin/python
import os,sys
import commands
import glob
path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')
for bamfile in bamfiles:
fullpath = os.path.join(path,bamfile)
txtfile = commands.getoutput('/share/bin/samtools/samtools ' + 'view '+ fullpath)
line=txtfile.readlines()
print line
this samtools view will produce (I think) .txt file
I got the errors:
Traceback (most recent call last):
File "./try.py", line 12, in ?
txtfile = commands.getoutput('/share/bin/samtools/samtools ' + 'view '+ fullpath)
File "/usr/lib64/python2.4/commands.py", line 44, in getoutput
return getstatusoutput(cmd)[1]
File "/usr/lib64/python2.4/commands.py", line 54, in getstatusoutput
text = pipe.read()
SystemError: Objects/stringobject.c:3518: bad argument to internal function
Seems it's the problem with commands.getoutput
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 subprocess
从命令文档中:
更新:刚刚意识到您正在使用Python 2.4。执行命令的一个简单方法是 os.system()
I would recommend using subprocess
From the commands documentation:
Update: Just realized you're using Python 2.4. An easy way to execute a command is os.system()
在谷歌上快速搜索“SystemError:Objects/stringobject.c:3518:内部函数的错误参数”会出现几个错误报告。例如 https://www.mercurial-scm.org/bts/issue1225 和http://www.modpython.org/pipermail/mod_python/2007-June/023852.html。这似乎是 Fedora 与 Python 2.4 结合的问题,但我不太确定。我建议您遵循 Michael 的建议并使用 os.system 或 os.popen 来完成此任务。为此,您的代码中的更改将是:
A quick google search for "SystemError: Objects/stringobject.c:3518: bad argument to internal function" brings up several bug reports. Such as https://www.mercurial-scm.org/bts/issue1225 and http://www.modpython.org/pipermail/mod_python/2007-June/023852.html. It appears to be an issue with Fedora in combination with Python 2.4, but I am not exactly sure about that. I would suggest that you follow Michael's advice and use os.system or os.popen to accomplish this task. To do this the changes in your code will be: