在子进程调用ls期间发生错误 [英] Error during subprocess calling ls

查看:378
本文介绍了在子进程调用ls期间发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 追溯(最近的最后一次呼叫):
文件< pyshell#2>,第1行,< module>
subprocess.call([ls,-l])
文件D:\Python27\lib\subprocess.py,第522行,调用
返回Popen(* popenargs,** kwargs).wait()
文件D:\Python27\lib\subprocess.py,第710行,__init__
errread,errwrite)
文件D:\Python27\lib\subprocess.py,第958行,在_execute_child
startupinfo)
WindowsError:[错误2]系统找不到指定的文件


解决方案

您正在调用 ls 命令在Windows上,没有一个 ls 。您可以从 ls 的端口rel =nofollow noreferrer > http://gnuwin32.sourceforge.net/packages/coreutils.htm ,并将其安装的bin目录添加到您的Windows PATH (或将其添加到PATH中运行python脚本的时间,如果您不知道如何执行此操作,请参阅这个这个问题分别)。



dir 是Windows等效的,但它会打印不同格式的输出相比 ls ,可能这个调用是从目录中获取某种信息。在python中有更好的方法来做到这一点。如果您可以识别出要尝试获取的信息(例如许可位),那么您可以编写一个纯粹的python版本(或者为执行此操作的Windows找到等效的python代码段),而不依赖外部命令。



有趣的是, subprocess.call()只需运行命令并返回错误代码。 ls 本身只是打印信息,所以如果这是意图(只是为了向用户显示目录在控制台中的内容),那么可以用 DIR 。有时,您将需要根据您调用的命令提供 shell = True 作为参数。请注意文档中的警告。如果它尝试提取信息,它可能想要执行 subprocess.Popen() subprocress.check_output()样式调用,实际上可以使用被调用程序的输出。



如果例如,只是获取目录中的文件/目录列表和/或他们的时间戳,那么这是一个非常圆的方式获取这些信息,他们都可以在python本身完成,例如 os.walk() os.fstat () ,而在Python3中, pathlib 是获取目录中的文件信息的一些方法。


any idea what is the error trying to say?

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    subprocess.call(["ls", "-l"])
  File "D:\Python27\lib\subprocess.py", line 522, in call
    return Popen(*popenargs, **kwargs).wait()
  File "D:\Python27\lib\subprocess.py", line 710, in __init__
    errread, errwrite)
  File "D:\Python27\lib\subprocess.py", line 958, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

解决方案

You are calling the ls command on windows, which doesn't have an ls. You can grab a port of ls for windows from http://gnuwin32.sourceforge.net/packages/coreutils.htm and add it's installed bin dir to your windows PATH (or add it to PATH at run time of your python script. if you dont know how to to do this, see this and this question respectively).

dir is the windows equivalent, but it will print differently formatted output compared to ls, and presumably, this call is to get some kind of information from a directory. There are better ways to do this in python. If you can identify what information it is trying to get (e.g. permission bits), then you might be able to write a pure python version of this (or find an equivalent python snippet for windows that does this) and not rely on external commands.

Interestingly, subprocess.call() simply runs the command and returns the error code. ls itself just prints information, so if that is the intent (just to show the user a directory's contents in a console), then you can replace it with a call to dir. Sometimes, you will need to provide shell=True as a parameter depending on the command you are calling. Pay attention to the warning in the documentation though. If it is trying to extract information, it probably wants to do a subprocess.Popen() or subprocress.check_output() style call, where you actually can consume the called program's output.

If for example, it is just to get the list of files/directories in a directory and/or their timestamps, then this is a very round-about way of getting this information, and they can all be done in python itself e.g. os.walk(), os.fstat(), and in Python3, pathlib are some ways to get file information in a directory.

这篇关于在子进程调用ls期间发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆