简体   繁体   中英

redirecting the output of a python command to a txt file working in cmd but not in .bat file, how to solve?

well I'm trying to make a logger for my python script using a runner in a '.bat' format, that executes the script and saves an output file; without me having to do it manually.

when I tried to run my python script, script.py, and pass 20 as an argument for the script as well as redirecting the output to a log_file.txt, using windows command prompt, it worked just fine, and the log file was created. ~the cmd command:

python script.py 20 >> log_file.txt

But when I tried to run the same code using the runner ".bat" file it didn't work.

~The codes I've written inside the "runner.bat" is as follows

python script.py 20 >> log_file.txt
pause

~but the execution command is done by the bat file was-as seen from the screen-:

C:\Users\dahom\Desktop\folder>python script.py  1>>log_file.txt

I expected the ".bat" file to behave the same save the log_file as the cmd terminal. But when I ran the bat file it didn't redirect the output to the log_file.txt But it seems to be running the script, without but one indication that it takes some time for the script running. note: both the batch file and the script are in the same folder/dir/path.

HERE is an image showing everything.

TRY:

@echo off
 "C:\Users\dahom\AppData\Local\Programs\Python\Python37-32\python.exe" "C:\Users\dahom\Desktop\new.py" >> "C:\Users\dahom\Desktop\log_file.txt" & type "C:\Users\dahom\Desktop\log_file.txt" 
pause

NEW.py:-

print("Echo Fox")

OUTPUT OF THE BATCH SCRIPT:-

Echo fox
Press any key to continue . . .

WORKING:-

Just Provide the full paths of each file used in the command (python exec, python script, text file etc) . When the command get's pipe'd to file use & type "file_path" to display the contents of the file after writing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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