How to open a python file with cmd - python

Hi have been working on a project and I have been trying to open a python file with python. So far I have learnt that it is not possible but I have learned that you can do it with cmd. I have got code that can open cmd but I can't seem to work out how to get cmd to open a python file, also if that is possible I would also like to close cmd when the python file is opened. This is what I have so far:
import os
os.system('cmd /k "Python"')

import os
os.system('cmd /k "python filename.py"') # just add your filename
You need to provide your filename to run the python file same as you do when you need to run a python file in cmd/terminal.
If you need to open the python file instead of run that file you can do:
import os
os.system('cmd /k "start notepad++ filename.py"') # open file with notepad++
or
os.system('cmd /k "more filename.py"') # to view file content in cmd
to open file with sublime:
os.system('cmd /k "subl.exe filename.py"') # make sure you have store sublime path in environment variable.

To open a file refer to the documentation at https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
But your intention is not clear. If you want to read it and evaluate it, then you might want to reconsider your approach (but it is possible).
If you want to run code dynamically checkout exec
If you want to start a sub-process in the OS then checkout
https://docs.python.org/3/library/subprocess.html

Related

How to write this type of code in "vim test.py"

"!spleeter separate -o output/ converted.mp3"
I want to add this code to my python file where I have converted the video file into an audio file and now I am separating the music from vocals. This code works properly on the command line but I am confused about how to use it in a VIM file so that it may run simply by running the command "python3 test.py".
Thank you in advance
If you want to run terminal commands in python use subprocess library in python.
import subprocess
subprocess.run(['!spleeter', 'separate', '-o', 'output/', 'converted.mp3'])

How can I make python open a cmd and set : cd and then command that I need to use

I want to make script that will open and pre set cmd with scrcpy commands.
I tried modules like os,subprocess,pyautogui
When I tried to open cmd with os and type inside it with pyautogui it didnt work
What should I use to type commands.
All I need to write is 'cd "directory"' and scrcpy but I cant find a way to do it with python
You can use following code:
import os
os.system('cmd /c "Your Command Prompt Command"')
This is the code to do it
import os as os
os.startfile("cmd.exe")
And to run the command use
os.system("Your Command")
This will show the command you passed into the cmd terminal.

running bash script from python file

I have a bash script which changes the path on my command line,
This one,
#!/usr/bin/env python
cd /mnt/vvc/username/deployment/
I have a python script which i wish to run after the path changes to the desired path,
The script,
#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])
for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)
I get this
File "/home/username/new_file.sh", line 2
cd /mnt/vvc/username/deployment/
^
SyntaxError: invalid syntax
Any suggestions on how can i fix this?thanks in advance
You need to explicitly tell subprocess which shell to run the sh file with. Probably one of the following:
subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])
However, this will not change the python program's working directory as the command is run in a separate context.
You want to do this to change the python program's working directory as it runs:
os.chdir('/mnt/vvc/username/deployment/')
But that's not really great practice. Probably better to just pass the path into os.listdir, and not change working directories:
os.listdir('/mnt/vvc/username/deployment/')

Open python shell in the current directory

I want to open python shell in the current directory like cmd. My system is windows 10. How can I achieve that?
Something like that:
Suppose that I open a directory in D disk, let's say the path is "D:\PythonProjects". I tried to open cmd in current directory, in cmd I type
"python" to get python shell, but the work directory of python didn't chage.
Did you try something like this in command line?
C:> D:
D:> cd PythonProjects
D:\PythonProjects> python
[Something about Python interpreter]
>>>
You can just simply open shell
and than
import os
os.chdir(" ")
In between quotes you can specify path were you want to open that
You can also see current path by
os.getcwd()

Executing batch file from Python causes infinite loop

I have this python code:
import os
os.system("cleanup.bat")
and this is my cleanup.bat file:
sc delete service1
sc delete service2
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Key1" /f
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Key2" /f
EXIT
However, when I run my python code, my batch file loops infinitely. What is causing this?
On my system, there already is a cleanup.bat in system32. It looks like a leftover of some installation.
Do not rely on the path nor the file extension association. A BAT file is not executable, it is interpreted by cmd.exe.
Change your Python code to this
import os
os.system("cmd.exe /c .\\cleanup.bat")
And run it without relying on file extension association, like this
python cleanup.py

Categories

Resources