python - 子进程中断管道和文件忙

标签 python linux bash shell

我正在尝试使用 python 来做一些文件系统的事情,因为我不想处理复杂的 shell 脚本并且宁愿尽可能将我的所有编程限制在 python 中。 “search_string”中的 shell 命令读取目录中的文件名并将前 10 个名称写入文件。

search_string = "find " + rootDir + "/"+str(k)  +" -iname \"*\" -type f | head -10  >> negatives" + str(i) + ".txt"
print(search_string)
subprocess.call(search_string, shell=True)

此代码适用于我的 ubuntu 14.04 电脑,但不适用于最终必须运行的 aws,并给出错误:

find: `standard output': Broken pipe
find: write error

ubuntu@ip:$ uname -r
3.13.0-37-generic

所以我决定将长 shell 命令写入一个我认为很容易调用的文件(在使用 chmod 命令使 shell 脚本文件可执行之后):

search_string = "sudo find " + rootDir + "/"+str(k)  +" -iname \"*\" -type f | head -10  >> trainingfiles/negatives" + str(i) + ".txt"
f=open("cmd.sh","w")
f.write(search_string)
f.flush()
os.fsync(f.fileno())
f.close
p1=subprocess.Popen(["chmod","+x","/home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout, stderr = p1.communicate()
output = p1.communicate()[0]
print('stdout:'+stdout+' stderr:'+stderr)
sys.stdout.flush()

p2=subprocess.Popen(["sudo /home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout, stderr = p2.communicate()
print('stdout:'+stdout+' stderr:'+stderr)
sys.stdout.flush()

但是我明白了

stdout: stderr:
Traceback (most recent call last):
File "prepare_and_train.py", line 56, in <module>
 p2=subprocess.Popen(["/home/www-data/web2py/applications/fingerPrint/modules/classifier_stuff/cmd.sh"],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 26] Text file busy

如果我将 PIPE 更改为 STDOUT,我会得到一个有趣的结果

OSError: [Errno 9] Bad file descriptor

当我尝试 subprocess.call 时同样的“文件忙”:

sudo: unable to execute ./cmd.sh: Text file busy
stdout: stderr:

我真的不在乎我是怎么做到的,我只想要工作代码 - 这里有什么提示吗?我(很可能)是 linux 的新手

最佳答案

您遇到的错误正在发生,因为您正试图在脚本仍处于打开状态以供写入时执行它。特别是,请参阅以下最小示例:

#!/usr/bin/env python

import os

f = open('f.sh', 'w')
f.write("#!/bin/sh\necho test")
os.chmod('f.sh', 0o755)
os.execl('f.sh', './f.sh')

如果你执行它,你会得到:

$ ./a.py 
Traceback (most recent call last):
  File "./a.py", line 8, in <module>
    os.execl('f.sh', './f.sh')
  File "/usr/lib64/python3.4/os.py", line 491, in execl
    execv(file, args)
OSError: [Errno 26] Text file busy

如果您确保在执行前关闭文件,例如:

#!/usr/bin/env python

import os

with open('f.sh', 'w') as f:
    f.write("#!/bin/sh\necho test")
os.chmod('f.sh', 0o755)
os.execl('f.sh', './f.sh')

它工作正常:

$ ./a.py 
test

进入您的具体问题,第 6 行:

f.close

您缺少括号,因此您没有调用 close() 方法,您只是获取(而不是使用)它。那应该是:

f.close()

或者在我的例子中最好是 with 语句。

同时,您可能还想使用 os.chmod() 而不是调用外部 chmod 工具。

关于python - 子进程中断管道和文件忙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625166/

相关文章:

python pgdb挂数据库

使用 GaussianNB 分类器进行交叉验证的 Python 朴素贝叶斯

mysql - Erlang 及其堆内存消耗

bash - 字符串同时大于零且为空

r - 让 Rscript 读取或从标准输入获取输入

mysql - Bash 脚本 : Mysql query field to variable

python - 投票分类器 : Different Feature Sets

python - 为什么没有打印出评论?我使用了正确的 HTML 元素吗?

android - Linux 蓝牙找不到具有 UUID 的 Android 服务

python - 使用 Bash 脚本从文件夹名称中删除前 4 个字母