页面“Python中执行系统命令常见的几种方法”与“Cannot access Input/output error”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
docker>Evan
 
 
第1行: 第1行:
=执行系统命令=
+
=trouble=
<pre>  
+
<pre>
(1)os.system  
+
内网机器 PHP的人 随便重启,然后机器再起来出事了
 +
ls /home
 +
cannot access Input/output error
 +
 
 +
和乱码
 +
d??????????  ? ?    ?        ?            ? home
 +
 
 +
</pre>
 +
=shooting=
 +
<pre>
 +
lsof  | grep home
 +
有关home分区的程序先kill掉
 +
 
 +
 
 +
然后关掉机器 ,再硬重启 
 +
如果不行就得 
 +
fsck
 +
 
 +
例如如果上面的 /home  at  /dev/sda1
 +
1.应该是采用 修复命令  fsck 
 +
fsck -s /dev/sda1
 +
意外断电导致/dev/sda1分区的文件系统崩溃,所以针对这个分区进行修复,如果其他分区也出现类似情况,可以进行全盘修复
 
   
 
   
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 
 
# 如果再命令行下执行,结果直接打印出来 
 
例如: 
 
>>> import os 
 
>>> os.system('ls') 
 
chk_err_log.py CmdTool.log  install_log.txt  install_zabbix.sh  manage_deploy.sh  MegaSAS.log 
 
 
(2)os.popen 
 
 
#该方法不但执行命令还返回执行后的信息对象 
 
#好处在于:将返回的结果赋于一变量,便于程序的处理。 
 
例如: 
 
>>> import os 
 
>>>tmp = os.popen('ls *.sh').readlines() 
 
>>>tmp 
 
['install_zabbix.sh\n', 'manage_deploy.sh\n', 'mysql_setup.sh\n', 'python_manage_deploy.sh\n', 'setup.sh\n'] 
 
 
(3)使用模块subprocess 
 
使用方法: 
 
>>> import subprocess 
 
>>> subprocess.call (["cmd", "arg1", "arg2"],shell=True) 
 
好处在于:运用对线程的控制和监控,将返回的结果赋于一变量,便于程序的处理。 
 
如获取返回和输出: 
 
 
import subprocess 
 
p = subprocess.Popen('ls *.sh', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 
 
print p.stdout.readlines() 
 
for line in p.stdout.readlines(): 
 
    print line, 
 
retval = p.wait() 
 
 
(4)  使用模块commands模块 
 
常用的主要有两个方法:getoutput和getstatusoutput 
 
>>> import commands 
 
 
>>> commands.getoutput('ls *.sh') 
 
'install_zabbix.sh\nmanage_deploy.sh\nmysql_setup.sh\npython_manage_deploy.sh\nsetup.sh'
 
 
>>> commands.getstatusoutput('ls *.sh') 
 
(0, 'install_zabbix.sh\nmanage_deploy.sh\nmysql_setup.sh\npython_manage_deploy.sh\nsetup.sh') 
 
 
注意: 当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess,如果使用os.popen则会出现错误。
 
</pre>
 
=参考=
 
[http://wangwei007.blog.51cto.com/68019/1106857 Python中执行系统命令常见的几种方法]
 
  
[https://my.oschina.net/renwofei423/blog/17403 Python执行系统命令的方法 os.system(),os.popen(),commands]
+
2.机器重启
 +
 
 +
 
 +
</pre>
 +
=see also=
 +
[https://unix.stackexchange.com/questions/39905/input-output-error-when-accessing-a-directory “Input/output error” when accessing a directory]
  
[http://www.361way.com/python-exec-system-command/3669.html Python执行系统命令的方法]
+
[https://www.cnblogs.com/Alanf/p/7509268.html cannot access Input/output error]
[[category:python]]
 

2020年2月18日 (二) 08:27的版本

trouble

 内网机器 PHP的人 随便重启,然后机器再起来出事了 
ls /home 
cannot access Input/output error

和乱码
d??????????   ? ?    ?        ?            ? home

shooting

lsof  | grep home 
有关home分区的程序先kill掉 


然后关掉机器 ,再硬重启  
如果不行就得  
fsck

例如如果上面的 /home  at  /dev/sda1
1.应该是采用  修复命令  fsck  
fsck -s /dev/sda1
意外断电导致/dev/sda1分区的文件系统崩溃,所以针对这个分区进行修复,如果其他分区也出现类似情况,可以进行全盘修复
 

2.机器重启 


see also

“Input/output error” when accessing a directory

cannot access Input/output error