简体   繁体   English

通过批处理文件运行时,将python脚本的输出打印到Windows控制台

[英]Printing output of python script to Windows console, when running via batch file

I have tried to look at other resources on SO but none seem to solve my problem: 我试图查看SO上的其他资源,但似乎没有一个能解决我的问题:

Executing Python Script From Command Line is Hiding Print Statements 从命令行执行Python脚本隐藏了打印语句

How do I write output in same place on the console? 如何在控制台的相同位置写入输出?

Python script doesn't print output in command prompt Python脚本无法在命令提示符下打印输出

I have a .py script that I'm executing via a batch file. 我有一个通过批处理文件执行的.py脚本。 Within my .py script, I have a few different attempts at printing output to the console window. 在我的.py脚本中,我做了几次尝试将输出打印到控制台窗口的尝试。 When I run the batch file and the console window pops up, I don't see the print output. 运行批处理文件并弹出控制台窗口时,看不到打印输出。 I don't have any other problems with executing the program, and I've otherwise used python and batch files to generate file output, etc. 我在执行程序时没有其他问题,否则我使用了python和批处理文件来生成文件输出,等等。

This is my .py script, I'm trying three different ways of writing output based on other SO articles: 这是我的.py脚本,我正在尝试基于其他SO文章的三种不同的写输出方式:

import sys

# Attempt 1
print('hello')

# Attempt 2
sys.stdout.write('hello')

# Attempt 3
if __name__ == '__main__':
    print('hello')

My batch file: 我的批处理文件:

"C:\Program Files\Anaconda3\pythonw.exe"  "\\*********\printTest.py"
PAUSE

My output, none of my desired 'hello' output displays. 我的输出,没有显示我想要的“ hello”输出。 I've edited the location of my script but the directory has been triple checked and is correct: 我已经编辑了脚本的位置,但是目录已被三重检查,并且是正确的:

C:\Windows>"C:\Program Files\Anaconda3\pythonw.exe"  "\\********\printTest.py"

C:\Windows>PAUSE
Press any key to continue . . .

How do I get 'hello' to print to the console? 如何获得“ hello”打印到控制台?

You're running "pythonw.exe", which is for graphical user interface (GUI) scripts, not console scripts. 您正在运行“ pythonw.exe”,它用于图形用户界面(GUI)脚本,而不是控制台脚本。 Run "python.exe" to inherit CMD's console for standard I/O. 运行“ python.exe”以继承CMD的标准I / O控制台。

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

相关问题 通过批处理文件运行时,如何将 python 脚本的 output 打印到 Windows 控制台? - How to print output of python script to Windows console, when running via batch file? 运行Windows批处理文件,vbscript / wscript或Python控制台脚本时,将图像显示为初始屏幕 - Display an image as a splash screen when running Windows batch file, vbscript/wscript or Python console script 在Python脚本中运行批处理文件时出现Windows错误2 - Windows Error 2 when running batch file in Python script 通过批处理文件运行 abaqus python 脚本? - Running an abaqus python script via batch file? 将计划任务(PYTHON脚本而非批处理文件)保存到控制台 - Save Scheduled Task (PYTHON Script NOT Batch file) Console Output to a file Python 放入批处理脚本时文件未运行 - Python File is not running when put in a batch script 从另一个python文件运行python脚本时如何显示控制台输出 - How to show console output when running a python script from another python file 在 python shell 从 Z0F4137ED1502B5045D6083AA258B 文件5C42Z 中运行 python 脚本 - Running python script in python shell from a windows batch file Jenkins 不在控制台中打印 python 脚本的输出 - Jenkins not printing output of python script in console 从 windows 批处理文件运行 python 脚本 - 文件名冲突 - Running python script from windows batch file - filename conflicts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM