【发布时间】:2015-12-31 14:37:52
【问题描述】:
我在 python 中使用“sh”模块来调用 Linux 上的外部命令。在我的特殊情况下,我想调用“du”命令,因为它比“手动”进行此类计算更有效。不幸的是,以下行不起作用:
output = sh.du('-sx', '/tmp/*')
但这确实有效:
output = sh.du('-sx', '/tmp/')
如果我传递一个星号,我会收到以下错误消息:
'ascii' codec can't encode character u'\u2018' in position 87: ordinal not in range(128)
有人知道如何处理命令行参数中的星号吗?
根据要求,这是堆栈跟踪:
Traceback (most recent call last):
File "./unittest.py", line 33, in <module>
output = sh.du('-sx', '/tmp/*')
File "/usr/local/lib/python2.7/dist-packages/sh.py", line 1021, in __call__
return RunningCommand(cmd, call_args, stdin, stdout, stderr)
File "/usr/local/lib/python2.7/dist-packages/sh.py", line 486, in __init__
self.wait()
File "/usr/local/lib/python2.7/dist-packages/sh.py", line 500, in wait
self.handle_command_exit_code(exit_code)
File "/usr/local/lib/python2.7/dist-packages/sh.py", line 516, in handle_command_exit_code
raise exc(self.ran, self.process.stdout, self.process.stderr)
sh.ErrorReturnCode_1
【问题讨论】:
-
您的临时文件夹中是否存在使用任何超出 ASCII 范围的字符的文件?完整的回溯会很有用。
-
你忘记了异常中最重要的部分,告诉你没有这样的文件或目录
-
不在 /tmp 但我假设在其他地方。但这应该不是问题,因为我使用“-s”选项运行“du”。 '/tmp' 在这里只是一个例子。在我的输出中,没有任何字符会超出 ASCII 范围。
-
@RegisMay,它与 char 无关,它发生在每个目录中,因为你传递了一个不被解释为一个的通配符,你可以在任何地方运行它,你会得到相同的输出。最后,除非您使用 sudo 运行脚本,否则您可能无权从 /tmp/ 读取,因此无论如何您都会看到错误
标签: python linux shell command-line sh