I had to do an Applescript and by the end of it i just want to run a python script.
I wrote
do shell script "/Users/Tom/Desktop/ayscript.py"
But it's said "permission denied"
Any idea ?
You are trying to run the script directly instead of using Python to run it. You need to do shell script python, passing the path to your script as an argument.
do shell script "python /Users/Tom/Desktop/ayscript.py"
Related
I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))
I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])
I have a very simple bash script test.sh as shown below
#!/usr/bin/env bash
mkdir "/c/AAA"
I want to execute this code in python. When I call os.system(r"Y:\test.sh") in python, a window pops up and asks me which program I want to open the test.sh with. Then python will end with output 0 and no folder is created in my C drive. I can't find any solution online. Any help will be appreciated. :)
os.system() will invoke your command the same as windows cmd would, in this case, the windows doesn't know how to execute *.sh files, so it opens it's default dialog so you can pick one program that you know can ran it.
The same will happen if you open windows terminal and try to invoke such file.
If your windows have a bash interpreter try invoking it like this:
os.system("bash Y:\test.sh")
Instead of running this with a native-Windows Python interpreter, run it with a Cygwin copy of Python, which has an os.system() that will be invoked with the Cygwin /bin/sh.
I would like to execute a circleCN.py python script by using #!/bin/sh.
I have searched the internet, found some suggestions, and none work. I get permission denied, and I changed my chmod of circleCN.py with a+x, I get invalid syntax, or I get another error. I have tried . /path/circleCN.py, exec /path/circleCN.py, python -c "/path/circleCN.py" and none work.
I have also tried to change #!/bin/sh to #!/usr/bin/env python and had no success. I would like to keep #!/bin/sh though.
I am very new to shell programming. I am also new to Linux.
When OpenFOAM users write Allrun or Allclean scripts, they use the #!/bin/sh shebang. I am working on an automation script and I would like to conform to the standard.
Why do you need to execute python script under a bash shebang?
You can invoke python script inside the shell script like this
#!/bin/sh
python /path_to_file/circleCN.py
I am having problems executing a shell script using subprocess.call() and I made this test in Python console to try to figure out which is the problem:
import subprocess
subprocess.call(["touch", "/tmp/out.txt"])
This works and creates the file in tmp folder. However, none of these two calls work:
subprocess.call(["sh", "/tmp/test.sh"])
subprocess.call(["/tmp/test.sh"])
/tmp/test.sh:
#!/bin/bash
touch out.txt
exit 0
Basically, executing an script from subprocess.call() is not producing any output. I gave full permissions to files and folders to avoid any problem. It seems the problem may be related to the user executing the script, but it is the same user as in the first case, which is working.
Any idea what the problem could be?
BTW, I am using Ubuntu 12.04 LTS, Python 2.7.4.
Thanks in advance.
Your script is executed correctly. The problem is with your bash script.
You have to understand that this script inherits working directory of the parent script. Therefore out.txt will be created in the directory where you run the python script which makes use of subprocess.call.