Maya ignores my doubles quotations - python

I'm having troubles starting maya from a python script with a mel command. Or rather I have a problem getting the mel command to run, maya launches just fine.
This is what the maya documentation says about starting with a mel command:
-command [mel command]
Runs the specified command on startup. The command should be enclosed
in double quotes to protect any special characters, including spaces.
Whatever I try Maya just ignores my doublequotes and gives me a syntax error.
This is my code:
import os
dir = "D:\exampleProject\maya"
os.system('maya.exe -command \"setProject \"'+dir+'\"\"')
I figure this would be read as this in maya: setProject "D:\exampleProject\maya" (which is what I want)
What I get is instead: setProject D:\exampleProject\maya which generates a syntax error in maya due to the lack of "" around the directory path.

Answer from comments
From the MEL documentation, it states that "Every statement in MEL must end with a semi-colon (;)."
MEL strings also require quotations to be escaped, therefore double escape the internal quotations.
'maya.exe -command \"setProject \\\"'+dir+'\\\";\"'

If all you need is to set the project you can use the startup flag "-proj"
maya.exe the/scene/iwant/to/open.ma -proj the/project/root
See the Documentation

Related

How can I enter "&" into my Java Scanner without getting a (presumed) bash error? [duplicate]

I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.
The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've attempted quoting the command, escaping the ampersand with ^ and neither works. I'm now out of ideas. Any suggestions?
To clarify from current responses:
I am using the subprocess module
I am passing the command line + arguments in as a list
The issue is with the path to the command itself, not any of the arguments
I've tried quoting the command. It causes a [Error 123] The filename, directory name, or volume label syntax is incorrect error
I'm using no shell argument (so shell=false)
In case it matters, I'm grabbing a pipe to stderr for processing it, but ignoring stdout and stdin
It is only for use on Windows currently, and works as expected in all other cases that I've tested so far.
The command that is failing is:
p = subprocess.Popen(prog, stderr = subprocess.PIPE, bufsize=-1)
when the first element of the list 'prog' contains any ampersands. Quoting this first string does not work.
Make sure you are using lists and no shell expansion:
subprocess.Popen(['command', 'argument1', 'argument2'], shell=False)
Try quoting the argument that contains the &
wget "http://foo.com/?bar=baz&baz=bar"
Is usually what has to be done in a Linux shell
To answer my own question:
Quoting the actual command when passing the parameters as a list doesn't work correctly (command is first item of list) so to solve the issue I turned the list into a space separated string and passed that into subprocess instead.
Better solutions still welcomed.
"escaping the ampersand with ^"
Are you sure ^ is an escape character to Windows? Shouldn't you use \?
I try a situation as following:
exe = 'C:/Program Files (x86)/VideoLAN/VLC/VLC.exe'
url = 'http://translate.google.com/translate_tts?tl=en&q=hello+world'
subprocess.Popen([exe, url.replace("&","^&")],shell=True)
This does work.

Is there a way to make a VBA code run a Python code?

I have read all online questions on this problem and none of them seem to be working for me. I want to write a code in VBA such that when a button is pressed, my code in Python automatically starts running.
I am getting a file not found error when I run the code but I have checked the path and I know it is correct.
The code I am trying is:
Sub MyMacro()
Shell("C:/Users/RGilsburg/New folder/pythonw.exe" & "F:/Asset/Global/Port/untitled1.py")
End Sub
Can anyone tell me where the error is?
The shell command should work, however, you have some issues with the command to be executed:
You need to put a blank between the Python command and the python file.
Use the backslash, not the slash if you are on windows
If a file contains a blank, you have to put it in Double quotes ". When you want to have a quote character in a string in VBA, you have to double it.
Basically, the parameter to Shell should be the same as you would enter it on a command prompt. To check this, write the command into a string variable and write this to the immediate window (Ctrl+G). Copy it from there and paste it into a CMD-window.
Try
Dim python as string, script as string, cmd As String
python = """C:\Users\RGilsburg\New folder\pythonw.exe"""
script = "F:\Asset\Global\Port\untitled1.py"
cmd = python & " " & script
Debug.print cmd
Shell cmd
this may be a bit ignorant of me since i know nothing of python and little of vba but did you add python as a reference in the vba editor.
if not
go to the tools dropdown in the vba editor select references and see if you can find python.

Why does Python's os.system() command on Windows XP require two double quotes at the beginning to run?

I've encountered some very strange behavior on Windows XP. I'm using Python to execute a command to open a browser using a shortcut file in a folder on the desktop.
The following line is what I expect to do the job:
os.system(r'"C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" "chrome.google.com/webstore"')
It's a raw string literal so all the backslashes are actual backslashes. I can tell that is true by putting echo at the start of that command. (i.e. os.system('echo "C:\Documents and Settings\blah\blah chrome.google.com/webstore"') )
Using echo returns the following:
"C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" "chrome.google.com/webstore"
That looks like a fine Windows command, yes? Well it is. Copying and pasting that into a command prompt runs fine. But the actual command (without echo) fails. The error states that
'C:\Documents' is not recognized as an internal or external command.
Which is a pretty standard error for an unquoted path. But wait, the command we echoed was good, so it should run, right? I guess not...
Through trial and error I was able to find something that worked. The following line is the only way I've been able to get the browser to launch:
os.system('""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"')
That's right, apparently the solution is to add an extra double quote at the beginning of the command and take out the double quote before the second argument.
To me that looks like empty string, unquoted path with unescaped spaces, then a quoted url that starts with a space.
If I echo that command it returns exactly what you would expect:
""C:\Documents and Settings\you\Desktop\Chrome Browsers\Google Chrome 46.lnk" chrome.google.com/webstore"
But it works! Pasting that echo result into the command line fails with the "C:\Documents not recognized" error from before, but the Python command opens the browser to the correct page anyway.
Could someone please explain what is happening here? I am really confused by this behavior because it is not at all what I expect.
P.S. This behavior is entirely different on every Windows OS past XP. For Vista and newer the command is:
os.system(r'"C:\Users\you\Desktop\Chrome Browsers\Google Chrome\Google Chrome 46.lnk" "chrome.google.com/webstore"')
Because there are " " spaces in your paths. C:\Documents and Settings\.. see the 2 spaces? otherwise it will pick up C:\Documents as a binary and and as the first param, Settings\.. as the other param.. and so on. This way youre saying: this whole thing is a binary C:\Documents and Settings\.. and chrome.google.com/webstore is my argument.
Make sense?

How to run a command line in Python

I am trying to execute
"C:/Program Files/AnsysEM/AnsysEM15.0/Win64/Designer.exe" -runscriptandexit "C:/Python27/simula_SIR_Phyton.py"
that is a to run a script in a program and I am not able to do it. I have succeed to run a single file like:
os.startfile("C:/Users/amrodri.UPVNET/Desktop/Scripts/SIR_europea_script.adsn")
But I have not succeed with the other problem. Can anyone help?
I have tried among others:
os.system("C:/Program Files/AnsysEM/AnsysEM15.0/Win64/Designer.exe" -runscriptandexit "C:/Python27/simula_SIR_Phyton.py")
os.system takes a single string as an argument. In order to have double quotes within a Python string (without terminating the string), you need to escape them using a backslash, like this:
os.system("\"C:/Program Files/AnsysEM/AnsysEM15.0/Win64/Designer.exe\" -runscriptandexit \"C:/Python27/simula_SIR_Phyton.py\"")
Or, alternatively, use single quotes instead:
os.system("'C:/Program Files/AnsysEM/AnsysEM15.0/Win64/Designer.exe' -runscriptandexit 'C:/Python27/simula_SIR_Phyton.py'")
See:
os.system()
Using quotes at the command line (This is Unix-specific, but should also apply to Windows if you're using something like PowerShell)
the culprit here is the space between Program and files. In windows, when you want to execute an address with an space in it, you need to put it between "", which is going to get mixed with Python's quotations. An easy solution would be to use raw '' in Python. For example:
import os
ansysedt_exe = r'"C:\Program Files\AnsysEM\AnsysEM16.0\Win64\ansysedt.exe" -runscriptandexit C:\automation\test_1.py'
print ansysedt_exe
os.system(ansysedt_exe)
Please notice that the designer address was put between "c:\...\designer.exe" because of the space in program files folder name, but we don't have to do the same for the script address, because there is no space there. Also just a heads up, in R16, designer.exe is going to be merged with AnsysEDT.exe.

Dropping a file onto a python script in windows - avoid Windows use of backslashes in the argument

I know all about how Windows uses backslashes for filenames, etc., and Unix uses forward. However, I never use backslashes with strings I create in my code. However:
When windows explorer "drops" a file onto a python script, the string it passes contains backslashes. These translate into escape sequences in the strings in the sys.argv list and then I have no way to change them after that (open to suggestions there)
Is there any way I can somehow make windows pass a literal string or ... any other way I can solve this problem?
I'd love my script to be droppable, but the only thing preventing me is windows backslashes.
EDIT:
Sorry everyone, the error was actually not the passing of the string - as someone has pointed out below, but this could still help someone else:
Make sure you use absolute path names because when the Windows shell will NOT run the script in the current directory as you would from a command line. This causes permission denied errors when attempting to write to single-part path-names that aren't absolute.
Cannot reproduce. This:
import os, sys
print sys.argv
print map(os.path.exists, sys.argv)
raw_input()
gives me this:
['D:\\workspaces\\generic\\SO_Python\\9266551.py', 'D:\\workspaces\\generic\\SO_Python\\9254991.py']
[True, True]
after dropping the second file onto the first one. Python 2.7.2 (on Windows). Can you try this code out?

Categories

Resources