简体   繁体   English

Windows上的Python STDOUT

[英]Python STDOUT on Windows

Python on Windows does not use normal STDOUT, so what is going on here? Windows上的Python不使用普通的STDOUT,那么这是怎么回事?

python --version
Python 2.7.15

shows a version! 显示一个版本! But I can't capture it! 但是我抓不到它!

python --version > temp.txt
Python 2.7.15
type temp.txt

NOTHING! 没有!

The issue is, I need to do logic depending on the Python version (from JavaScript) and it's been pretty hopeless so far. 问题是,我需要根据Python版本(来自JavaScript)来进行逻辑处理,到目前为止,这已经毫无希望了。

在将stdout重定向到文件的同时,尝试将stderr重定向到stdout:

python --version 1>temp.txt 2>&1

You can use a Python command to print the version info to stdout: 您可以使用Python命令将版本信息打印到stdout:

> python -c "import sys;print(sys.version)" > temp.txt

After this command, temp.txt will contain the version information. 执行此命令后, temp.txt将包含版本信息。

> type temp.txt
2.7.15 |Anaconda, Inc.| (default, May  1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)]

If you wanted to get fancier, you could use sys.version_info to extract only specific part of the version, like the major number as explained in https://stackoverflow.com/a/9079062/7517724 . 如果您想变得更高级,可以使用sys.version_info来仅提取版本的特定部分,例如https://stackoverflow.com/a/9079062/7517724中说明的主要编号。 Using the following line will store only the major version number in temp.txt : 使用以下行将仅在temp.txt存储主要版本号:

> python -c "import sys;print(sys.version_info[0])" > temp.txt
> type temp.txt
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM