How to open text file in notepad with cmd window in background? - python-2.7

I want to open a text file without opening cmd window in the background. I have tried:
webbrowser.open('file.txt')
but it crashes ArcGIS so I tried following:
os.system('file.txt')
it opens text file without crashing ArcGIS but cmd window remains in the background and goes away when I close text file.
It is more of a display choice question and just checking if there is any suggestion to avoid cmd window in the background.

I don't know if this is what you want, but maybe you should create .bat file (something like here) and run this with Python subprocess.

Save your script with a .pyw extension and the console window won't appear.
From the Python documentation :
On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
You need to modify the program that calls the "add-in script" to run it with pythonw.exe (and not python.exe which is the default).

Related

Command window closing very quickly when running batch script with system()

I have an application developed using Borland C++Builder (Embarcadero nowadays) on Windows 10.
I want to launch a script contained in a .bat file from my app, using
the following code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
system("myfile.bat");
}
However, the command window appears then exits very fast, and I have no time to see the result.
I have added a pause command in the .bat file, but without success.
Below is the code for my .bat file:
#echo off
"./ttpmacro.exe" /I "./binary.ttl"
pause
Is there any solution to this issue?
I have resolved my issue by changing the path of the working directory. I have modified the path directory of the file "mybat.file" and then all things got OK.

How do I run an exe file from cmd but in the same window?

I want to run an exe file in the cmd.
After I compile a c++ file I want to see the executable, so I type .build\bin\test.exe. The first time it opens me a new cmd window
and when I try again it runs on the same window.
How do I solve this?
Thx

How to close the cmd window after the batch file run

I want to close the command prompt after the bat file run. From the bat file it's calling two python scripts and I have tried basic exit and many solutions to close the cmd in the bat file but non of were not working.
c:\python27\python.exe C:\Python27\EconomyNext\DetailEconomyNews.py %*
c:\python27\python.exe C:\Python27\EconomyNext\DetailFinanceNews.py %*
exit
Did you check this post:
CMD Script: How to close the CMD
It has a few options depending on how you are calling the CMD like:
exit /b
or
start c:\python27\python.exe
C:\Python27\EconomyNext\DetailEconomyNews.py %* &&
c:\python27\python.exe C:\Python27\EconomyNext\DetailFinanceNews.py %*
&& exit
A little late, but another option which worked from https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Running .BAT or .CMD files in minimized mode
To run a batch file in a minimized window state, follow these steps:
Create a shortcut to the .BAT or .CMD file. To do so, right click on the file, click Send To, Desktop (create shortcut)
Right click on the shortcut and choose Properties
In the Run: drop down, choose Minimized
Click OK
Double-click the shortcut to run the batch file in a minimized window state.

How to run an exe file in a completely hidden way

I'm able to execute an exe file via command prompt using below line:
process = subprocess.Popen("LogCollector.EXE ")
But the LogCollector.EXE GUI is still visible, Please suggest some method to run this completely in hidden way.
import os
os.system('start /MIN notepad.exe');
Use this command in the window, it will run the application and minimize the application.
Maybe it will help you

Strange CMD errors only when CMD is opened from my program

This is a weird one for sure.
If I open a command prompt window directly (searching cmd in start, right click > open command window here, cmd within bat file, etc....) all commands entered run perfectly fine.
If I open a command prompt window from within my C++ application (system("cmd"); or QProcess::startDetached("cmd"); etc....) the commands I enter throw errors.
Here are a few commands that don't work in the cmd opened from my app:
vssadmin delete shadows /all
vssadmin list shadows
wmic
shadowcopy
and so on... I get Class not registered and Initialization failure errors all around. Anything to do with shadow copies isn't working at all. But again, the weird thing is, those same commands work perfectly fine when cmd was opened traditionally (not from a program). Both instances of cmd have admin privileges.
So my question is, how come the way I open cmd affects whether or not some commands work? Everything I can see says there should be no difference.
32-bit applications running on WOW64 will be put under file system redirection. Therefore if your app is a 32-bit one, the call system("c:\\windows\\system32\\cmd.exe"); will be redirected to C:\Windows\SysWOW64\cmd.exe and 32-bit cmd will always be invoked. You have some solutions:
Use system("c:\\windows\\sysnative\\cmd.exe"); to access the real system32 folder and get the 64-bit cmd
Turn off file system redirection explicitly (should be avoided in general)
Or better compiling it as a 64-bit app.