I have a python script 'test.py' which takes arguments in batches of 50-100-150 etc.
Everytime I have to open multiple terminal windows (10-15 windows) and execute 'test.py' providing batches manually (which is exhaustive after some period of time)
I decided to write a separate script which can open terminal window recursively by providing arguments in batches.
The short script runs fine but throwing error if there's space in path to script file. I've tries numerous solutions provided on internet but none of them seems to work
Short code snippet: from applescript import tell yourCommand = 'python3 ~/Desktop/untitled folder/test.py <range of batch, Ex: 1 51>' tell.app( 'Terminal', 'do script "' + yourCommand + '"')
What I've tried so far:
- Enclosing folder with spaces in double quotes, Ex: "untitled folder"
- Using "r", Ex: yourCommand = r'"python3 ~/Desktop/untitled folder/test.py <range of batch, Ex: 1 51>"'
- Using backslashes, Ex: untitled\ folder
- Using double backslashes, Ex: untitled\ folder
But everytime either the terminal window is not opening or if it is opening, it is throwing error "/Library/Frameworks/Python.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users//Desktop/untitled': [Errno 2] No such file or directory"
Env: Mac OSX
Any help would be appreciated. I am sure I must be making some silly mistake, but not able to resolve it.
Screenshots:
script 1.py script 1.py
output of 1.py output of 1.py
-
Tried as you suggested but this is not opening TERMINAL window nowkartik mittal– kartik mittal2022年12月06日 15:48:41 +00:00Commented Dec 6, 2022 at 15:48
-
Hi @RobertKniazidis, I have 2 py scripts in 'untitled folder' i.e. 1.py and test.py, 1.py contains script which launches new terminal window and supposed to execute command stores in variable 'yourCommand' 1.py is executed from terminal window I hope I am able to narrate the issue in a better manner now!!!kartik mittal– kartik mittal2022年12月07日 13:37:54 +00:00Commented Dec 7, 2022 at 13:37
2 Answers 2
when using do shell script:
do shell script "python3 ~/Desktop/untitled\\ folder/test.py"
when typing in the Terminal, or using do script command:
tell application "Terminal"
do script "python3 ~'/Desktop/untitled folder/test.py'"
end tell
The best practice is using quoted form to escape. Duplicating folder example in the Terminal:
set theSource to "/Desktop/TESTFiles/"
set theDestination to "/Desktop/TEST Files/"
tell application "Terminal"
do script "ditto ~" & quoted form of theSource & " ~" & quoted form of theDestination
end tell
The same task in the do shell script along with the quoted form requires determining the home folder path before. That is, instead of the tilde:
set home to POSIX path of (path to home folder)
set theSource to quoted form of (home & "/Desktop/TESTFiles/")
set theDestination to quoted form of (home & "/Desktop/TEST Files/")
do shell script "ditto " & theSource & " " & theDestination
11 Comments
Save following python script as print trianlges.py file in untitled folder folder of desktop.
max_size = 10
print(
"(a)" + " " * (max_size) +
"(b)" + " " * (max_size) +
"(c)" + " " * (max_size) +
"(d)" + " " * (max_size)
)
for i in range(1, max_size + 1):
print("*" * i, end = " " * (max_size - i + 3))
print("*" * (max_size - i + 1), end = " " * (i - 1 + 3))
print(" " * (i - 1) + "*" * (max_size - i + 1), end = " " * 3)
print(" " * (max_size - i) + "*" * i)
Now, run following command in the Terminal to print triangles. As you can see, the path has spaces in the folder name and in the file name, and works fine:
python3 ~'/Desktop/untitled folder/print triangles.py'