命令模块问题

发布于 2024-11-30 08:14:12 字数 992 浏览 2 评论 0原文

你好,我正在尝试通过导入命令模块在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-12-07 08:14:12

我建议使用 subprocess

从命令文档中:

自版本 2.6 起已弃用:命令模块已在 Python 3.0 中删除。请改用 subprocess 模块。

更新:刚刚意识到您正在使用Python 2.4。执行命令的一个简单方法是 os.system()

I would recommend using subprocess

From the commands documentation:

Deprecated since version 2.6: The commands module has been removed in Python 3.0. Use the subprocess module instead.

Update: Just realized you're using Python 2.4. An easy way to execute a command is os.system()

猫卆 2024-12-07 08:14:12

在谷歌上快速搜索“SystemError:Objects/stringobject.c:3518:内部函数的错误参数”会出现几个错误报告。例如 https://www.mercurial-scm.org/bts/issue1225http://www.modpython.org/pipermail/mod_python/2007-June/023852.html。这似乎是 Fedora 与 Python 2.4 结合的问题,但我不太确定。我建议您遵循 Michael 的建议并使用 os.system 或 os.popen 来完成此任务。为此,您的代码中的更改将是:

import os,sys
import glob

path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')

for bamfile in bamfiles:
    fullpath = os.path.join(path,bamfile)
    txtfile = os.popen('/share/bin/samtools/samtools ' + 'view '+ fullpath)
    line=txtfile.readlines()
    print line

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:

import os,sys
import glob

path= '/home/xxx/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')

for bamfile in bamfiles:
    fullpath = os.path.join(path,bamfile)
    txtfile = os.popen('/share/bin/samtools/samtools ' + 'view '+ fullpath)
    line=txtfile.readlines()
    print line
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文