在命令提示符下运行Python并记录到Fi

2024-10-04 15:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我使这个简单的.bat批处理文件包含

@echo off
set log=mainLog.txt
cd /d F:/Project/Python
echo [%TIME%]starting main.py >%log%
python main.py >> %log%

命令提示符成功地将main.py输出重定向到日志文件,但没有在提示屏幕上显示。所以我把它改成:

^{pr2}$

这将向提示屏幕显示main.py的输出,但不会重定向到日志文件。我已经试过使用:

python main.py 2>> %log% 1>> %log%

但这给了我:

The Process cannot access the file because it is being used by another process

假设我的main.py包含:

for i in range(10):
    print("this is line no. {}".format(i))

有什么办法吗?在


Tags: 文件pyechotxtprojectlogismain
1条回答
网友
1楼 · 发布于 2024-10-04 15:26:36

使用键入在提示中调用和显示文件(仅文本)

阅读更多如何使用类型-@SS64 - Type Command。在

(例如)

此脚本输出到日志,但您需要调用输出日志以在提示时显示。在

@echo off
::script file path - x:\myproject\xyz.py
::script filename if batch runs in same directory - xyz.py
set myscrpt="xyz.py"

::Log file path
set pylog="myscriptlog.log"

::Python Path
pushd C:\Python27

::script output to log
echo      log-start      >%pylog%
%myscrpt% >>%pylog%
echo      log-end      >>%pylog%
cls

::Display log output in prompt
type %pylog%
pause>nul
exit

相关问题 更多 >

    热门问题