I'm trying to run a simple windows command dir E:\Projects\ML /s /b using subprocess.Popen function without using Shell=True, my sample program is below
import subprocess
import os
import time
input='E:\Projects\ML'
command = "dir "+input+' /s /b '
os.system(command)
status=subprocess.Popen(command.split(' '),shell=False,text=True,cwd=input,stdout=subprocess.PIPE)
while status.wait()!=0:
time.sleep(1)
print(status.communicate())
with os.system() the command is giving appropriate results but with Popen() I'm facing below problem
Traceback (most recent call last):
File "e:\Projects\ArchiveViewer\PythonR&D\EncriptionWithPopen\runLocalCMD.py", line 9, in <module>
status=subprocess.Popen(command.split(' '),shell=False,text=True,cwd=input,stdout=subprocess.PIPE)
File "C:\Users\nmaiya\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\nmaiya\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I know that if I set shell=True the command will run, but is there any reason why it is not working with shell=Fales
dir is an internal command inside cmd.exe, there is no dir.exe that can be started as a process.
system("xyz") is internally executed as something like cmd.exe /C xyz on Windows.
Related
I'm training a Yolov5 model with Torch on Jupyter Notebook using this line of code:
!python train.py --img 416 --batch 16 --epochs 50 --data dataset.yaml --weights yolov5s.pt --cache
but I'm getting this error that says:
Traceback (most recent call last):
File "C:\Users\suzan\Desktop\CSPPEDA\yolov5\train.py", line 29, in <module>
import torch
File "C:\Users\suzan\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\__init__.py", line 123, in <module>
raise err
OSError: [WinError 1455] The paging file is too small for this operation to complete. Error loading "C:\Users\suzan\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\lib\caffe2_detectron_ops_gpu.dll" or one of its dependencies.
So I searched for answers online and found a solution from Chris O'Bryan (How to efficiently run multiple Pytorch Processes / Models at once ? Traceback: The paging file is too small for this operation to complete), however I'm getting this error in Command Prompt as I run the fixNvPe.py python script:
Failed to write modified DLL! [[WinError 5] Access is denied: 'C:\\Users\\suzan\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\torch\\lib\\torch_cuda_cu.dll']
I looked back at the explanation from the first solution by Chris, in which he said "If it is under Program Files, you will need to run the script with administrative privileges". So I run the command prompt as adminstrator and typed in net user administrator /active:yes, then on the normal command prompt, and rerun the fixNvPe.py python script.
After rerunning it, I tried to train the Yolov5 model again, but the same error OSError: [WinError 1455] The paging file is too small for this operation to complete. still exists.
Am I running the script in administrative priviledges the correct way? What should I do? I apologize for the question as I'm a beginner in this stuff.
gcloud.cmd is a Windows command-line script. I am trying to run it from the Bash shell installed on Windows 10. It is recognized by the CMD prompt, but not by “Bash for Windows 10”.
Based on this thread I created a .bashrc file with this entry:
PATH=$PATH:/mnt/c/Users/username/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin
It seems that Bash now finds the file because when I run gcloud.cmd it shows:
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 1: #echo: command not found
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 2: rem: command not found
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 7: syntax error near unexpected token `newline'
/mnt/c/Users/***/AppData/Local/Google/Cloud SDK/google-cloud-sdk/bin/gcloud.cmd: line 7: `rem <cloud-sdk-cmd-preamble>'
I also tried the following commands because Cygwin seems to recognize .bat files automatically:
cmd.exe gcloud.cmd
cmd gcloud.cmd
cmd gcloud
cmd /c gcloud
cmd /c glcoud.cmd
All of the above commands show:
No command 'cmd' found, did you mean: (…)
How do I run Windows Batch commands from Bash?
You need to specify the full path to cmd.exe.
I have added the following to my ~/.bash_aliases:
alias cmd='/mnt/c/Windows/System32/cmd.exe /c'
with this you can run *.bat files with:
$ cmd ./test.bat
Sorry to post this as an answer but I cannot yet comment. I was thinking that maybe you could have a batch file that calls bash scripts, then once they exit, it starts to bat files, then starts more bash scripts? You could split your code in too... Just an idea.
I am using python 3.3 and in my code I need something to open cmd.exe with the following arguments and run it. The desired line in cmd.exe should be:
C:\Program Files (x86)\GlobalMapper15>global_mapper.exe script.gms variable
I saw different answers but the most I managed with subprocess.call is to open either cmd.exe, or global_mapper.exe. I did not manage to obtain the line above in cmd.exe.
I tried so far:
#import subprocess
import os
os.system("cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe script.gms")
#subprocess.call(["cmd.exe C:\Program Files (x86)\GlobalMapper15\global_mapper.exe", "script.gms"])
Neither of them worked well.
Of course, it would be great if the line would be also executed. Can anyone help me make this happen?
Thank you all,
To run global_mapper.exe in given directory:
#!/usr/bin/env python
from subprocess import call
dir = r"C:\Program Files (x86)\GlobalMapper15"
cmdline = "global_mapper.exe script.gms variable"
rc = call(cmdline, cwd=dir) # run `cmdline` in `dir`
If you want to start the command in a new console window:
rc = call("start cmd /K " + cmdline, cwd=dir, shell=True)
maybe with this:
import os
os.system("program.exe -parameter")
you can Create a .bat file and call it
dir = r"C:\Program Files (x86)\GlobalMapper15"
write=''.join((dir,'\\','call.bat'))
f= open(write,'w')
with f:
f.write('#ECHO ON\n Call global_mapper.exe script.gms variable'
subprocess.call(write,cwd=dir)
I have read everywhere that the subprocess module is the best way to use the command line from Python, but I'm having trouble making it work. I have a firefox extension called Page Saver that saves an image of an entire webpage. On the comand line, this command successfully saves an image:
firefox -savepng "http://www.google.com"
I've tried this script to automate the process, but no luck:
import subprocess
subprocess.call(['firefox', '-savepng', 'http://www.google.com'], shell=False)
I get this error:
Traceback (most recent call last):
File "C:/Users/computer_4/Desktop/Scripts/crescentsaver.py", line 2, in <module>
subprocess.call(['firefox', '-savepng', 'http://www.google.com'], shell=False)
File "C:\Python27\lib\subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Am I using subprocess incorrectly?
Thanks.
Update:
Found the solution. It was a little detail of the extension I was using. The script had to be run from the Firefox default saves folder to work. I also ran the parameters as a string, not a list, with shell=True:
import subprocess
subprocess.call('firefox -savepng http://www.google.com', shell=True)
I could not answer my own question due to the limitation on new users answering their own questions within eight hours of posting.
What subprocess.call() is telling you with that error is that it cannot find the firefox command on your PATH. (Which is a list of directories where Windows searches for commands).
So you have two options here:
Specify the full path to the Firefox command in your script: this is easy, just put the full path in your python code.
Add the directory that contains the Firefox command to your PATH. This is a little more complicated but you can find a decent tutorial on Super User
I am trying to create a python program to easily change my cmd startup folder(rather than typing cd ... to navigate to the desired file)
But first I need to figure out how to change it without having to type regedit.exe into cmd.
After going through python documentation this is where I got:
from winreg import*
a=OpenKey(HKEY_CURRENT_USER,"Software\Microsoft\Command Processor\\")
SetValue(HKEY_CURRENT_USER,"Software\Microsoft\Command Processor\\",REG_SZ,"cd\\the path that I want.")
This code does edit the string value (I believe that is what its called) Default.
But what I need it to do is to edit the string value Autorun
#I tried diffrent ways of putting Autorun in that SetValue function but it didn't work.
Note: both Default and Autorun are in HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
I've also tried
SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")#Don't know if this is the right way to use it.
But this gives me this error:
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")
WindowsError: [Error 5] Access is denied
I use python 3.1 and windows7
Thank you in advance.
You have to use SetValueEx and also open the key with appropriate access rights, either KEY_WRITE or KEY_ALL_ACCESS, like this:
from winreg import*
a=OpenKey(HKEY_CURRENT_USER,"Software\\Microsoft\\Command Processor",0,KEY_WRITE)
SetValueEx(a,"Autorun",0,REG_SZ,"cd\\The path that I wantsss.")
CloseKey(a)