I am making use of Beyond Compare 3 to see the difference between two XML files. I am willing to make a small python script which on executing will open files ready to compare in Beyond Compare tool.
So far I tried invoking BC3 from command line syntax as below and it works:
BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"
but when I try to execute same syntax from python script as shown below, It throws error
from subprocess import check_output
check_output('BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"', shell=True)
The error which is shown is:
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"' returned non-zero exit status 1
am I missing something? I tried different solutions to open command line instructions using this tutorial and many others but its not working.
Do something like this. Give Absolute path of .exe
check_output(absolute_path_of_beyond_compare "c:\Ref-2.xml" "c:\Cop-2.xml"', shell=True)
I am able to open the Beyond Compare using following code:
from subprocess import check_output
check_output("BCompare.exe Test1.txt Test2.txt", shell=True)
where BCompare.exe path is added in path variable and Test1.txt Test2.txt are present in the same directory from where i have executed the program.
import subprocess
subprocess.Popen([r"C:\Program Files\Beyond Compare 4\BCompare.exe", r"FullFilePath_File1", r"FullFilePath_File2"])
I tested on mine, and it works.
Instead of checkout, I am using Popen.
I am using Jupyter notebook by installing Anaconda3-5.2.0-Windows-x86_64
Python version = 3.6.5
use exact path where the beyond compare is installed or add that to your environment variable "Path".
in case using exact path of installation put something line "\"C:\Program Files\Beyond Compare 4\BCompare.exe\" test1.txt test2.txt"
the \" enables to read the special characters and extra spaces in path
Related
The goal is to open a particular excel file using a python shell subprocess. The code cannot be more simple yet I cannot figure out what is wrong:
import subprocess
arg1 = "C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\EXCEL.EXE"
arg2 = "C:\\Users\\user\\Documents\\test.xlsm"
p = subprocess.Popen(["start", arg1, arg2], shell=False)
The command works perfectly directly on the shell but when done through subprocess throws the following error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have also tried the below which works equally fine directly on the shell but the behavior is different:
p = subprocess.Popen([arg1, arg2], shell=False)
The following failed assertion pops up from Excel:
snapshopt error
My next try was:
import os
os.system("C:\\Users\\user\\Documents\\test.xlsm")
Which replicates the same assertion error as above plus returns a code 3 which based on
System Error Codes (0-499)
is a path not found.
Again same path works on shell, at this stage I run out of ideas, any help?
os.startfile is actually a better way to do it. As specified in the documentation -
os.startfile(path[, operation])
Start a file with its associated application.
When operation is not specified or 'open', this acts like
double-clicking the file in Windows Explorer, or giving the file name
as an argument to the start command from the interactive command
shell: the file is opened with whatever application (if any) its
extension is associated.
When another operation is given, it must be a “command verb” that
specifies what should be done with the file. Common verbs documented
by Microsoft are 'print' and 'edit' (to be used on files) as well as
'explore' and 'find' (to be used on directories)
You can just do the following -
import os
os.startfile("C:\\Users\\user\\Documents\\test.xlsm")
I'm a COMPLETE beginner to python, I'm making my first python script that really does anything. I'm assigning a directory to a variable so I can use it to move a file, but the directory includes a folder starting with . and python says it's invalid syntax. So how can I get python to ignore the .?
EDIT: Here's the code
#!/usr/bin/env python
import os
Optifine = find /home/Sol33t303/.minecraft -name
OptiFine_1.10.2_HD_U_E3.jar
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
You are mixing two very different things.
Are you writing Python or Bash, cause this is totally Bash:
Optifine = find /home/Sol33t303/.minecraft -name
You can't just run Bash commands inside a Python script!
If, for example you want to run a shell command inside your script and get its output you can use:
Optifine = subprocess.check_output(['find', '/home/Sol33t303/.minecraft', '-name'])
And then you split the output by line, and foreach line (file found), move it to the desired destination:
for line in Optifine.splitlines():
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.
I have this simple script..that does not work
import subprocess
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "VBoxManage startvm WIN7"])
I have the same thing in a bat file...which works perfectly.
cd C:\Program Files\Oracle\VirtualBox
VBoxManage startvm "WIN7"
I have the VBoxManage.exe in the PATH of Windows 8.1 (My host OS).
The python script understands the VBoxManage executable and spits out it's manual and then this ..
Syntax error: Invalid command 'VBoxManage startvm WIN7'
Could you give me a way to start a VM from inside a python script, either by invoking the .exe directly or by running the .bat file ?
Note: I have searched for the vboxshell.py file but not found it anywhere...:[
subprocess.call() expects a list of arguments, like so:
subprocess.call(['C:\Program Files\Oracle\VirtualBox\VBoxManage.exe',
'startvm',
'WIN7'])
Your code passes 'VBoxManage startvm WIN7' as a single argument to VBoxManage.exe, which expects to find only a command (e.g. 'startvm') there. The subsequent arguments ('WIN7' in this case) need to be passed separately.
In addition, there is no need to repeat the executable name when using subprocess.call(). The example from the Python docs invokes the UNIX command "ls -l" as follows:
subprocess.call(['ls', '-l'])
In other words, you don't need to repeat the 'VBoxManage' part.
The trick is to pass the command as separate arguments
import subprocess
subprocess.call(["C:\Program Files\Oracle\VirtualBox\VBoxManage.exe", "startvm", "WIN7"])
I've installed the Ruby gem 'haml' on my mac, which I can use to compile haml files into html files using the following command at the terminal:
haml 'path/to/haml/file.haml' 'desired/html/path/file.html'
This command simply creates an html file at the second path, and gives no output in the terminal. So for example, this command:
haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"
Creates a sugar.html file at the given path. Now I'm trying to use this functionality from a python script. When I type this into IDLE's interactive python shell:
>>>import subprocess
>>>subprocess.Popen('haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"', shell=True, executable='/bin/bash')
<subprocess.Popen object at 0x159d6f0>
I get output suggesting that the process has been run, however, there is no file outputted. Why is this happening? I even put in the Shell argument, but no interactive shell shows up. Also, I read somewhere that the default shell used is not bash, which is what the Mac terminal uses, so I put that in too for good measure.
Following icktoofay's advice, I ran check_call. Here is the traceback I received:
Traceback (most recent call last):
File
"/Users/neil/Desktop/subprocesstest.py",
line 7, in
p = subprocess.check_call(x, shell=True) File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
line 504, in check_call
raise CalledProcessError(retcode, cmd) CalledProcessError: Command 'haml
"/Volumes/Macintosh
HD/Users/neil/Sites/ICSP/sugar.haml"
"/Volumes/Macintosh
HD/Users/neil/Sites/ICSP/sugar.html"'
returned non-zero exit status 127
According to the bash reference manual, while searching for a command to be executed,
If the name is neither a shell
function nor a builtin, and contains
no slashes, Bash searches each element
of $PATH for a directory containing an
executable file by that name. ... If
that function is not defined, the
shell prints an error message and
returns an exit status of 127.
However, I thought it was indeed finding the haml command after adding the shell and executable arguments, because before that, it was giving a 'file or directory not found error', which indicates that the function is not executable directly but rather in a shell only.
Now how do I make python find this haml command? Or would I have to use some ugly workaround like an applescript which then invokes the haml command.
I see that you are using, shell=True, so I would have expected things to just work. Checked it locally here with Python 2.7.1 and haml 3.1.1 and I had no problems executing it. There are also some python implementations you might be interested in, PyHAML, HamlPy, djaml or django-haml.
import subprocess
subprocess.Popen(['haml', 'hello.haml', 'hello.html'], shell=True)
% cat hello.html
<strong class='code' id='message'>Hello, World!</strong>
shlex.split() is your friend, when you want to build args list suitable for Popen and its ilk.
>>> import subprocess
>>> import shlex
>>> p = subprocess.Popen(shlex.split('haml "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.haml" "/Volumes/Macintosh HD/Users/me/Sites/ICSP/sugar.html"'))
>>> p.wait()
0