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.
Related
I am using a RSS program which uses tags, ignores and blacklists. I need to run python main.py -w [my webhook] -t [my tags] -i [my ignored users] -b [blacklisted tags] I simply saved that in a batch file so that I don't need to type it everytime in a terminal. After using it for like 1h I noticed that I gotta adjust everything till the RSS sends only that what I want, so I came up with a Idea to save the my tags etc. in seperate text files so that the batchscript reads the files and runs the command with my adjustments included. All the adjustments are seperated with commas (f.e. background,sky,rain). It would be even better if the program reads all the text files (tags are saved line by line in the text files so that every line has one tag because it gives me a better overview) and converts the ⏎ (enter) into a , before using it for the command. It would be nice of someone teaches me how to do that and explains their steps.
Update:
I found a batchscript and modified it so that it works for my scenario. The only problem is that the script doesn't send/grab the text if one text file contains any japanese characters (I changed the text file to ANSI because it would send otherwise only question marks). In my case it sends only python main.py -w [my webhook] -t ~1 -i [my ignored users] -b [blacklisted tags]. Instead of actually sending my tags from the tag list it sends just ~1. Does anyone know how to fix this and are there any unnecessary commands in my script or can I make it better?
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "tags="
for /F "usebackq delims=" %%T in ("Tag.txt") do set "tags=!tags!,%%T"
rem Remove comma at beginning of the tags value.
set "tags=!tags:~1!"
set "ignore="
for /F "usebackq delims=" %%I in ("Ignore.txt") do set "ignores=!ignores!,%%I"
rem Remove comma at beginning of the ignores value.
set "ignores=!ignores:~1!"
set "banned="
for /F "usebackq delims=" %%B in ("Banned.txt") do set "banned=!banned!,%%B"
rem Remove comma at beginning of the banned value.
set "banned=!banned:~1!"
python main.py -s [my sessionid] -w [my webhook] -t !tags! -i '!ignores!' -b '!banned!'
endlocal
pause
You should be able to import system from os and send the command through that. That way you can just write a separate python script around it that you can setup to run as you please. Assuming you're on windows, I'm not sure if this works on another os.
Using the following and replacing the variables with your arguments.
from os import system
webhook = ''
tags = ''
ignored_users = ''
blacklisted_tags = ''
system(f'python main.py -w {webhook} -t {tags} -i {ignored_users} -b {blacklisted_tags}')
I wasn't completely clear on what you were asking for so I hope this helps.
(Background: On an NTFS partition, files and/or folders can be set to "compressed", like it's a file attribute. They'll show up in blue in Windows Explorer, and will take up less disk space than they normally would. They can be accessed by any program normally, compression/decompression is handled transparently by the OS - this is not a .zip file. In Windows, setting a file to compressed can be done from a command line with the "Compact" command.)
Let's say I've created a file called "testfile.txt", put some data in it, and closed it. Now, I want to set it to be NTFS compressed. Yes, I could shell out and run Compact, but is there a way to do it directly in Python code instead?
In the end, I ended up cheating a bit and simply shelling out to the command line Compact utility. Here is the function I ended up writing. Errors are ignored, and it returns the output text from the Compact command, if any.
def ntfscompress(filename):
import subprocess
_compactcommand = 'Compact.exe /C /I /A "{}"'.format(filename)
try:
_result = subprocess.run(_compactcommand, timeout=86400,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,text=True)
return(_result.stdout)
except:
return('')
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>>.
I am using an EMACS package (vax) to calculate some tilings of various regions. At the moment I have a large number of text files and I open each one in EMACS, execute a single command,<Shift> 3, record the output to the minibuffer manually, and add eventually sum these output. This is incredibly time consuming, and I keep making errors.
I would really like to automate this process, could anyone give me some advice on how I might write some sort of script that will open each file in a specified directory, record the output of the minibuffer after entering the command <Shift> 3, and sum the successive outputs as it proceeds through the directory?
I am not familiar with LISP or EMACS, I've read through the tutorial for the latter. I have a rough working knowledge of how to code in Python, and if there is a way I could execute this all within a python script that would be really helpful.
Step by step instructions of how to run emacs in batch:
First you need to get the list of files that you want to operate on.
find-name-dired should be enough for most needs. Open dired
in the base directory of your project and M-x find-name-dired.
Accept the default for the base directory, enter for instance *.py as wildcard.
You now have a buffer with all file names that you're interested in.
Select them all with t. You can refine your selection with
m and DEL.
Start a shell command with !. Use this command template as a start:
emacs --batch ? --eval "(message \"%s %s\" (buffer-name) (buffer-size))"
Now you have all your output neatly in the shell output buffer.
The code above shows buffer size in characters for each file.
You should replace (buffer-size) with your code, i.e.
(if (equal vax-region (buffer-substring-no-properties (point-min) (point-max))) nil (vax-quit) (setq vax-region (buffer-substring-no-properties (point-min) (point-max)))) (catch (quote chromatic) (catch (quote singular) (vax-number)))
The whole thing should be on one line.
Alternatively, you can wrap the call to emacs --batch with a bash script
and call that instead from dired.
UPD: try to run this code
Instead of (buffer-size), put (progn (vax-mode) (call-interactively (vax-col vax-number))).
UPD: load vax.el
Use emacs --batch ? -l ~/path/to/vax.el --eval "(progn (vax-mode) (message \"%s %s\" (buffer-name) (call-interactively (vax-col vax-number))))".
I have a python script containing vraibles
example.py
"src = komponent/cool,
komponent/raw" /* path of source files */
and i have a batch file which needs to import the value of "src" for postporcessing
example.bat
--cs-include ='komponent/cool'* --cs-include ='komponent/raw'*
Is there any way to import directly between files (without using any other conversion) ?
"PyBat.bat" is one option but i am trying to figure out a better choice dont want to add on one more tool (not specifically)...as my project itself has too different files and interacting source.
Any help is appreciated..
Thank you in advance..!!!
In Python
Create a temp batch file in %TEMP%\setvars.bat containing SET commands to set environment vars.
You could use subprocess.POpen to run an ECHO something > path batch command so that %TEMP% can be used.
In Batch
Call the temp batch file : the vars are now available to use.
Solution is...
1.set a argument in the python itself.
2.call the batch file from python script and along with pass the argument using
os.system(cmd) and cmd as a string.
It will set the path itself.