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
Related
So basically I am running a script in Python that executes other 5 .py scripts as well, just like this:
exec(open('statcprs.py').read())
exec(open('dragndownf.py').read())
exec(open('lowprbubble.py').read())
exec(open('wshearstr.py').read())
exec(open('slices.py').read())
These .py files uses Paraview (another software) to run some stuff, so If I only run "statcprs.py", it will open Paraview's terminal and run the script. The problem is, from the first one "statcprs.py" to the second one "dragndownf.py" it doesn't interrupt the software, and it continue running it, interefering with scripts from both .py files.
I would like to execute the first one, stop and then start the second one from scratch without connection between them. is this somehow possible?
I think the problem is this line (line 1) which opens the terminal:
#!/usr/bin/env pvpython
The following will execute a list of python scripts in the same folder as the driver script:
import os
from pathlib import Path
import subprocess
import sys
scripts = [
'statcprs.py',
'dragndownf.py',
'lowprbubble.py',
'wshearstr.py',
'slices.py',
]
parent = Path(__file__).resolve().parent
for script in scripts:
script_path = parent / script
subprocess.call([sys.executable, script_path])
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
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/')
I am new to Python and Multiprocessing.
Summarized issue: I am running a .py executable which contains a Pool.map(function,list_args) the function it calls contain the line os.system() which it executes a .csh file in the same directory as the initial .py executable. This .csh executable creates a new folder and moves the working directory into it. I believe this where my code fails, I believe that the os.system() does not wait for the .csh executable to finish.
(Ubuntu is the OS on my computer).
Here is part of the code:
import sys
import os
import multiprocessing as mp
import time
def phenix(FILES):
os.system("mkdir temp_"+str(FILES))
time.sleep(1)
os.system("cp 5k10_only_atoms.pdb.mtz "+str(FILES)+" temp_"+str(FILES)+"/")
retval = os.getcwd()
time.sleep(1)
os.chdir(str(retval)+"/temp_"+str(FILES))
time.sleep(1)
os.system("./phenix.get_cc_mtz_pdb 5k10_only_atoms.pdb.mtz "+str(FILES)+" scale=True fix_xyz=True >> grabado")
os.chdir(retval)
#THE USER INPUTS VALUES FOR THE VARIABLES cores AND list_files
if __name__ == '__main__':
p = mp.Pool(cores)
p.map(phenix,list_files)
The command line
./phenix.get_cc_mtz_pdb 5k10_only_atoms.pdb.mtz .....and so on
Runs some calculations comparing values insides the files 5k10_only_atoms.pdb.mtz and FILES (another file e.g. "file-01"). It is this command line phenix.get_cc_mtz_pdb that creates another folder (called temp_dir) and moves the working directory into it to do some calculations and write out files.
Again I am pretty sure that this is where something goes wrong because the phenix command never does anything besides creating a folder named temp_dir. It is only when phenix (a .csh executable) tries to change the working directory that os.system() does not wait for any return and my .py executable continues on to the next command.
Is their some way to let my phenix executable finish its job with out os.system() not waiting for it to finish?
NOTE: I am not able to modify this phenix (.csh) executable. Also the phenix command line does work when I run it directly on the terminal.
I'm trying to use py2exe to compile a python script into an executable.
I've set up the setup.py file just like it's described in documentation:
from distutils.core import setup
import py2exe
setup(console=['agent.py', 'test.py'])
The agent.py file simply uses subprocess.Popen to open another script:
import sys
import subprocess
print 'is this working?'
child = subprocess.Popen([sys.executable, 'test.py'])
The test.py file is
while 0 == 0:
print 'test'
When running this as a python script, it works fine. When running as a py2exe-compiled executable, it does not run.
When I try to change the file reference in agent.py from 'test.py' to 'test.exe', running the compiled agent.exe simply prints 'is this working?' on an infinite loop. What have I done wrong?
sys.executable points to agent.exe instead of python.exe when run as compiled executable. You need to change your Popen to:
child = subprocess.Popen(['test.exe'])
when running compiled executable. You can use hasattr(sys, "frozen") to determine whether you're in frozen (py2exe) or not (Python script) mode.
That didn't quite work, but all I had to do was replace your answer with the full path name. Thanks! This worked:
app_path = os.path.realpath(os.path.join(
os.path.dirname(sys.executable), 'test.exe'))
child = subprocess.Popen(app_path)