I am redirecting the output of a Python script to a file.
It works fine if there is no error.
But if there is any error , I wish to capture the error in another file which is not happening now.
Below is the script which I have written.
#echo off
mode con cp select=65001
set dt=%Date:~10,4%-%Date:~4,2%-%Date:~7,2%
cd C:\API_DOC\softeon_project\script
python -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt
echo "after python path"
pause
exit
Any help would be appreciable.
The usage of dynamic environment variable DATE depends on Windows Region setting as defined for the currently used user account.
For example with getting Tue 12/26/2017 written into a command prompt window on running in same window echo %DATE% it is possible to use either
set "dt=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%"
or better
set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"
Both command lines use string substitutions to get environment variable dt defined with string 2017-12-26. The difference is that the first command line references the characters in date string from left while the second command line references them from right. Therefore the second command line works also with no abbreviated weekday at beginning.
The help output for command SET on running set /? in a command prompt window explains string substitutions as used here.
A region independent solution to get current local date in format yyyy-MM-dd would be:
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "dt=%%I"
set "dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"
This variant is explained in detail in answer on Why does %date% produce a different result in batch file executed as scheduled task?
The disadvantage is that WMIC takes more than a second to output the local date and time which makes this solution much slower than the solution using dynamic environment variable DATE.
I suggest to use:
#echo off
rem Define encoding UTF-8 for console.
%SystemRoot%\System32\mode.com CON CP SELECT=65001
rem Get current local date in format yyyy-MM-dd.
set "dt=%DATE:~-4%-%DATE:~-10,2%-%DATE:~-7,2%"
rem Change the current directory independent on current drive.
cd /D C:\API_DOC\softeon_project\script
rem Execute Python interpreter and redirect standard output messages
rem to file log_%dt%.txt and error messages to logerr_%dt%.txt.
python.exe -u softeon_main.py >>C:\API_DOC\softeon_project\log\log_%dt%.txt 2>>C:\API_DOC\softeon_project\log\logerr_%dt%.txt
echo "After python path"
pause
There was a trailing space in Python command line which is removed in code above. See the answers on Why does ECHO command print some extra trailing space into the file? and
Why is no string output with 'echo %var%' after using 'set var = text' on command line? why a trailing space in a batch file could result in an unexpected output into a file or even unexpected behavior on execution of a batch file.
And python was extended with file extension .exe to avoid that by chance a file python.bat or python.cmd is found first by Windows command interpreter as in this case the next line would not be executed anymore because batch files must be called from within a batch file with command CALL to return to calling batch file on finishing execution of called batch file.
Read also the Microsoft article about Using command redirection operators for an explanation of >> and 2>>.
Related
I'm passing the result of the execution of a command to python as input, like so:
$ python parse_ips.py "$(kubectl get configmap ...)"
This works fine when executing from the command line, however I'm now trying to edit the file using PyCharm. Therefore I need the escaped version of the result of this command which I can paste into PyCharm's debug configuration, as I can't execute the command in real-time like I can do on the command line.
However, I am struggling to find a way to replicate the escaping bash does behind the scenes, so I can use the result as an argument within the PyCharm configuration. Running the above kubectl command results in a multi-line string which includes spaces and quotes. When I paste this into PyCharm it just interprets it as multiple arguments. I'm looking for the escaped result, which I could paste directly into the command line, or into PyCharm's debug configuration, to achieve the same result with a fixed parameter for testing.
Any help would be greatly appreciated!
Edit: To clarify, I mean on the command line the result of the $(kubectl ...) command is passed into the python program as a single command line argument when it is surrounded by quotes ("$(kubectl ...)"). So in the python program, you can access sys.argv[1] and it will contain the entire execution output of $(kubectl get configmap ...). However, if I execute that command myself on the command line, the result is a multi-line string.
If I then copy the result of that into PyCharm (or even on the command line again), it is interpreted as many command line arguments. E.g. it would look something like this:
$ python parse_ips.py apiVersion: v1
data:
item1: ifconfig-push 127.0.0.0 255.255.0.0
item2: ifconfig-push 127.0.0.1 255.255.0.0
item3: ifconfig-push 127.0.0.2 255.255.0.0
...
And so on. This obviously doesn't work in the same way as it did before. So I am unable to test my program without making the kubectl call from the command line each time. I was looking to replicate what "$(kubectl ...)" gets converted into so it is able to pass the entire output as a single command line entry.
I am struggling to find a way to replicate the escaping bash does behind the scenes
Typically use printf "%q" to escape stuff.
printf "%q" "$(kubectl get configmap ....)"
This is printf as the bash builtin command. It differs from coreutils printf, and newest ones also support %q with different quoting style:
/usr/bin/printf "%q" "$(kubectl get configmap ....)"
Modern bash also has quoting expansion:
var="$(kubectl get configmap ....)"
echo "${var#Q}"
And there is also the quoting style outputted by set -x.
I would suggest to use a file:
kubectl get configmap ... > /tmp/tempfile
python parse_ips.py "$(cat /tmp/tempfile)"
With xclip you can copy command output straight to the X server clipboard, which is handy:
printf "%q" "$(kubectl get configmap ...)" | xclip -selection clipboard
# then in another window:
python parse_ips.py <right mouse click><select paste>
I'm currently trying run the shell script by using the os.system method in python.
Python code:
file = open("History.txt","w")
file.write(history)
os.system('./TFPupload.sh')
Shell script code:
#!/bin/sh
HOST="ftp.finalyearproject95.com"
USER='*****'
PASSWD='*****'
FILE='History.txt'
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
echo ">>>uploaded<<<\n"
exit 0
At first, when i tried to run the python code and shell script one by one it works perfectly. However, when i attempt to use python to run shell script, instead of uploading the 'History.txt' file that contains data into the database, the file uploaded is an empty file. When i check using 'nano History.txt', it does contain data, only when it passes the text file to the database will be empty. Why is it?
Use With statement to open files whenever possible .
with open("History.txt","w") as file :
file.write(history)
os.system('./TFPupload.sh')
with statement takes care of closing the fd on its own .
some Ref : What is the python "with" statement designed for?
I am attempting to run this .PY file from Command Prompt:
# Merge two .BSG files
# Starting block and world position are taken from the first file
# Example: "bsgmerge input.bsg output.bsg merged.bsg"
import io, sys
one = open(sys.argv[1]).readlines()
two = open(sys.argv[2]).readlines()
for n in [1,3,5,7,9,11,17,19,21,23]:
one[n] = one[n][:-1]+"|"+two[n].partition("|")[2]
open(sys.argv[3],"w").write("".join(one))
It is a program that takes a creation from the game Beseige and merges it with another saved creation so that opening the merged file results in both creations being present. If you want more details, you can read up on that here.
I am having trouble figuring out how to call this program from the command line. At first I thought the problem was me having Python 2 (it requires Python 3), so I uninstalled 2 and installed 3. This did not help.
What I am doing is entering the "python" command to pull up the Python environment within CMD, then entering the command to call the program based on the third comment in the file ("bsgmerge input.bsg output.bsg merged.bsg").
I tried using full file paths or simply changing to the correct directory before typing the "python" command and using only the file names, but so far I've had no luck.
When I am in the correct directory, then enter the Python environment, typing the command "bsgmerge 1.bsg 2.bsg M.bsg" (my existing files to be merged are 1.bsg and 2.bsg), this error occurs:
File "<stdin>", line 1
bsgmerge 1.bsg 2.bsg M.bsg
^
SyntaxError: invalid syntax
I took a Python course (which is why I used to have Python 2 on my machine) last fall, so I noticed that there is no "def" defining a function in the above code, which is something I've never encountered, so I'm thinking that is the root of my problems.
Thanks in advance for the help.
I was probably same problem with python launcher.
If you use Linux, first line shoud be:
#! /path/to/your/python/3
In Windows it some more complicated:
In registry by regedit change
HKEY_CLASSES_ROOT\Python.File\shell\open\command from "C:\Python27\python.exe" "%1" %* to "C:\Windows\py.exe" "%1" %*.
And first line of script shoud be:
#! python3
Now it shoud work properly.
I have 3 different files, one Python file and two .bat files. They communicate between each other (hopefully).
When I execute the "Process_Videos.bat" by itself (double clicking in the windows explorer) it works fine, but whenever I call it from the Python file it doesnt work at all, just says "press any button to continue..."
I really need to have this structure, calling the "Process_Videos.bat" from a Python file, since I am extracting some web info. The "pythonExecute.bat" just works as a trigger for the entire process.
Also I have tried the "subprocess" approach, but not working either.
The files and respective code:
pythonExecute.bat
python "D:\\tests\\pythonCall.py"
pythonCall.py
import os
os.system('D:\\tests\\3.asc\\Process_Videos_asc.bat')
Process_Videos.bat
#echo off
setlocal EnableDelayedExpansion
set "FolderBaseName=TestName"
set "DropBoxFolder=D:\tests\3.asc\myDropBoxFolder"
set "BaseOutputFolder=D:\tests\3.asc\TEMP"
for %%I in (*.png) do (
set "slaveName=%%~nI"
set "slaveName=!slaveName:~6!
set "OutputFolder=%BaseOutputFolder%_!slaveName!"
echo !slaveName!
md "!OutputFolder!" 2>nul
for %%J in (*.mp4*) do (
ffmpeg -i "%%~fJ" -i "%%~fI" -filter_complex overlay "!OutputFolder!\%%~nJ.mp4"
)
"C:\Program Files\WinRAR\rar.exe" a -cfg- -ep1 -inul -m5 "%DropBoxFolder%\%FolderBaseName%_!slaveName!" "!slaveName:~6!\*"
rd /S /Q "!OutputFolder!"
)
pause
You need to:
a) Invoke your batch file within the directory it is in, (e.g. by changing directory first), and
b) Get rid of the pause at the end of the batch file.
You should also consider replacing the batch file altogether - python can do all of the things that it does much more neatly.
The accepted answer to this SO question gives some very good tips.
(on os x 10.10.1)I am trying to use a paired-end merger (Casper) within a python script. i'm using os.system (don't want to use subprocess or pexpect modules). In my script here is the line that doesn't work:
os.system("casper %s %s -o %s"%(filein[0],filein[1],fileout))
#filein[0]: input file 1
#filein[1]: input file 2
#fileout: output prefix (default==casper)
Once my script is launched only the 2 first string parameters of this command are interpreted but not the third one, causing an output file with the default prefix name. Since my function is iterating through a lot of fastq files, they are all merged in a single "casper.fastq" file.
I tried to mess up with the part of the command that doesn't work (right after -o), putting meaningless string and still it is executed with no error and the default output, here is the "messed up line":
os.system("casper %s %s -ldkfnlqdskgfno %s"%(filein[0],filein[1],fileout))
Could anybody help in understanding what the heck is going on?
Print the command before execute it to check if your command wrapped correctly(like file name need to be quoted)
Execute your assumed output command directly to see if it is misinterpreted.