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
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.
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.
I've tried using the following commands to run my .bin file I have saved on my desktop. I'm able to navigate and run it using;
cd desktop
chmod +x ./two1.bin
./two1.bin
(two1.bin being the name of the file)
When it runs it displays each line in the terminal window and says "command not found" after it reads the first instruction on each line. It looks like this,
./two1.bin: line 1: 0010000000001001: command not found
./two1.bin: line 2: 0010001000001001: command not found
./two1.bin: line 3: 0010010000001001: command not found
Anyone have an idea on what I'm doing wrong? I've tried researching this online can't find the correct solution, thanks in advance.
I have the following syntax written in PSPP .sps file:
GET FILE = '... result.sav'
save translate
/outfile = '... data.csv'
/type = CSV
/REPLACE
/FIELDNAMES
/CELLS=LABELS.
where ... stand for the path of the files.
The Script works as expected, so when I open PSPP and run it, it opens the first file and saves it as another CSV file. However, I would like to do two more things:
Call this file from CMD (in Windows) so it will execute all command automatically and silently, without showing the PSPP windows.
Add a line to the syntax to terminate PSPP after execution.
Right now I can only type the name of the .sps file in the CMD and it opens it but does nothing else. I have looked in the official docs but couldn't find any solution for that.
Well, I found the answer myself:
In CMD I had to type:
"C:\Program Files\PSPP\bin\pspp.exe" C:\Users\...\Dropbox\MATLAB\atid\convert_to_csv.sps
And that's all. It runs PSPP in silent mode and creates the file as needed.
What I didn't know was that I need to first write the path of PSPP .exe file ("C:\Program Files\PSPP\bin\pspp.exe") before the name of the syntax file. All the rest just worked.
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)