马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如题
我在cmd里面写相应的命令,他可以正常显示
(发现一个,好像gcc前缀的都不能正常显示)C:\Users\summer\Desktop\codehome>gcc kfkff.c
gcc: error: kfkff.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
我在代码里面这么写,他只给我返回了空的字符串>>> os.popen('gcc kfkff.c').read()
''
请问用os.popen()函数为什么不能正常读取输出的内容
(我试了下,其他命令都可以)>>> os.popen('ipconfig').read()
'\nWindows IP 配置\n\n\n无线局域网适配器 本地连接* 3:\n\n 媒体状态 . . . . . . . . . . . . : 媒体已断开连接\n 连接特定的 DNS 后缀 . . . . . . . : \n\n无线局域网适配器 本地连接* 12:\n\n 媒体状态 . . . . . . . . . . . . : 媒体已断开连接\n 连接特定的 DNS 后缀 . . . . . . . : \n\n无线局域网适配器 WLAN:\n\n 连接特定的 DNS 后缀 . . . . . . . : \n 本地链接 IPv6 地址. . . . . . . . : xxxxxxxxxxxxxxxxxxxxxx\n IPv4 地址 . . . . . . . . . . . . : xxxxxxxxxx\n 子网掩码 . . . . . . . . . . . . : xxxxxxxxxxxx\n 默认网关. . . . . . . . . . . . . : xxxxxxxxxxxxxxxxx\n'
>>> os.popen('pip -v').read()
"\nUsage: \n pip <command> [options]\n\nCommands:\n install Install packages.\n download Download packages.\n uninstall Uninstall packages.\n freeze Output installed packages in requirements format.\n list List installed packages.\n show Show information about installed packages.\n check Verify installed packages have compatible dependencies.\n config Manage local and global configuration.\n search Search PyPI for packages.\n cache Inspect and manage pip's wheel cache.\n index Inspect information available from package indexes.\n wheel Build wheels from your requirements.\n hash Compute hashes of package archives.\n completion A helper command used for command completion.\n debug Show information useful for debugging.\n help Show help for commands.\n\nGeneral Options:\n -h, --help Show help.\n --debug Let unhandled exceptions propagate outside the\n main subroutine, instead of logging them to\n stderr.\n --isolated Run pip in an isolated mode, ignoring\n environment variables and user configuration.\n --require-virtualenv Allow pip to only run in a virtual environment;\n exit with an error otherwise.\n -v, --verbose Give more output. Option is additive, and can be\n used up to 3 times.\n -V, --version Show version and exit.\n -q, --quiet Give less output. Option is additive, and can be\n used up to 3 times (corresponding to WARNING,\n ERROR, and CRITICAL logging levels).\n --log <path> Path to a verbose appending log.\n --no-input Disable prompting for input.\n --proxy <proxy> Specify a proxy in the form\n [user:passwd@]proxy.server:port.\n --retries <retries> Maximum number of retries each connection should\n attempt (default 5 times).\n --timeout <sec> Set the socket timeout (default 15 seconds).\n --exists-action <action> Default action when a path already exists:\n (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.\n --trusted-host <hostname> Mark this host or host:port pair as trusted,\n even though it does not have valid or any HTTPS.\n --cert <path> Path to PEM-encoded CA certificate bundle. If\n provided, overrides the default. See 'SSL\n Certificate Verification' in pip documentation\n for more information.\n --client-cert <path> Path to SSL client certificate, a single file\n containing the private key and the certificate\n in PEM format.\n --cache-dir <dir> Store the cache data in <dir>.\n --no-cache-dir Disable the cache.\n --disable-pip-version-check\n Don't periodically check PyPI to determine\n whether a new version of pip is available for\n download. Implied with --no-index.\n --no-color Suppress colored output.\n --no-python-version-warning\n Silence deprecation warnings for upcoming\n unsupported Pythons.\n --use-feature <feature> Enable new functionality, that may be backward\n incompatible.\n --use-deprecated <feature> Enable deprecated functionality, that will be\n removed in the future.\n"
>>> os.popen('gcc -v').read()
''
>>>
本帖最后由 isdkz 于 2023-2-21 21:47 编辑
这是因为 os.popen('gcc kfkff.c').read() 是从 stdout 中读取,而报错信息是输出到 stderr,所以从 stdout 是读不到报错信息的,
你可以把 stderr 重定向到 stdout,
>>> os.popen('gcc kfkff.c 2>&1').read()
'gcc: error: kfkff.c: No such file or directory\ngcc: fatal error: no input files\ncompilation terminated.\n'
>>>
官方推荐使用 subprocess:>>> import subproces
>>> p=subprocess.Popen("gcc kfkff.c",stderr=subprocess.PIPE)
>>> p.stderr.read()
b'gcc: error: kfkff.c: No such file or directory\ngcc: fatal error: no input files\ncompilation terminated.\r\n'
>>>
|