python - 无法使用 win32 dir 命令和 Python 子进程查看文件

标签 python windows subprocess dir

我正在尝试读入一个文件并生成该文件的哈希值。在大多数情况下,这一切进展顺利。但是,有一些文件我无法处理。例如:

file("c:\\windows\\system32\\nvvsvc.exe")

产生错误:IOError: [Errno 2] No such file or directory: 'c:\\windows\\system32\\nvvsvc.exe

同样,命令:

subprocess.check_output(r"dir c:\windows\system32\nvvsvc.exe", shell = True)

产生错误:

File Not Found

Traceback (most recent call last):

File "< stdin >", line 1, in < module >

File "C:\Python27\lib\subprocess.py", line 544, in check_output

raise CalledProcessError(retcode, cmd, output=output)

subprocess.CalledProcessError: Command 'dir c:\windows\system32\nvvsvc.exe' returned non-zero exit status 1

我尝试了许多变体,包括尝试让 cacls.exe 承认那里有一个文件。但是,当我退出 python 实例 (exit()) 并执行 dir c:\windows\system32\nvvsvc.exe ,它返回那里确实有一个文件。

使用 calc.exe 进行的类似测试允许我从 python 内部查看它并对其进行哈希处理。

我正在运行 Python 2.7。我尝试使用管理权限启动 python(在命令提示符下以管理员身份运行),但这并没有改变任何东西。从 python 内部运行时,权限似乎发生了一些变化,以至于文件系统本质上假装该文件对于 python 来说不存在。

仅供引用,相关文件的所有者(还有其他类似文件)是系统,文件上的 cacl 允许从用户、管理员和系统读取该文件。

最佳答案

简单的回答! (但不一定容易找到)关于IOError,是因为不需要转义反斜杠。

第二部分看起来像权限错误,如上面的评论中所述。

关于python - 无法使用 win32 dir 命令和 Python 子进程查看文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069009/

相关文章:

c++ - chrome 和 firefox 浏览器不在 WWW-Authenticate header 中发送 kerbros token 以进行协商

windows - 将 bash 脚本编译成 Windows 可执行文件

python - 如何在 Pandas 中选择某些列索引(连续的和单独的)

c++ - 错误 : the application was unable to start correctly

python - 字符串值前面的 'u' 符号是什么意思?

python - 在 Python 中使用子进程复制 os.system 功能?

Java 和 Python - 从子进程启动时 System.console() 返回 null

Python子进程(错误?)合并ssh的stderr和stdout

python - pygame.init() 什么时候会返回 (6,0) 以外的东西?

python - 如何使用没有 ORM 的 SQLAlchemy 查询从表中删除行?