6

There may be something obvious that I'm missing here but searching google/so has not provided anything useful.

I'm writing a python script utilizes tkinter's filedialog.askopenfilename to open a file picker. Without getting into to much detail, I have the following line, which serves to bring the file picker to the front of the screen (taken directly from this helpful answer):

os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

As you can see from the above code snippet, this line is too long for pep8 guidelines and I'd like to break it down.

However, despite my best efforts I can't seem to get it to split. This is due (I think) to the fact that the line contains both single and double quotes, and unfortunately os.system seems to insist on it being a single line.

I've tried

  1. Triple quotes
  2. String literal patching (\ at end, and + at beginning of each line)
  3. Triple quotes on a per line basis

If it's relevant: using OSX and running python 3.6.4.

What is the correct (and ideally, minimal) way to go about breaking this line down?

Peter Dolan
  • 1,393
  • 1
  • 12
  • 26
  • 1
    I am not sure I understand the issue. Python automatically concatenates strings immediately following each other (without you having to put a `+` in between) so unless I'm severely mistaken, you should be able to simply split the string into multiple strings, each being on a separate line and being wrapped by single, double or triple quotation marks as you like (or as is needed). Also, you can always escape the single and double quotation marks contained in your string, so you are certainly not forced to use triple quotation marks. – balu Apr 26 '18 at 00:00
  • @balu I think I was being foolish here and forgetting you could backslash quotes to make them non literal – Peter Dolan Apr 26 '18 at 00:01
  • 1
    Hehe, as a matter of fact I just updated my previous comment to mention that, too. – balu Apr 26 '18 at 00:03
  • Great minds think alike! Thanks for the help :) – Peter Dolan Apr 26 '18 at 00:11

1 Answers1

5

Using the much improved subprocess module is usually a much better, more powerful, and safer way to call an external executable.

You can of course pass variables with \n in them as the arguments as well.

Note, the double (()) are because the first parameter is a tuple.

import subprocess
subprocess.call((
    '/usr/bin/osascript', 
    '-e',  
    'tell app "Finder" to set frontmost of process "Python" to true',
    ))

There are at times reasons to call through the shell, but not usually.

https://docs.python.org/3.6/library/subprocess.html

Peter Dolan
  • 1,393
  • 1
  • 12
  • 26
gahooa
  • 131,293
  • 12
  • 98
  • 101
  • 3
    You seem to be missing a parenthesis. – user2357112 Apr 25 '18 at 23:57
  • 1
    I'm trying to run `subprocess.call(('cp file.txt folder','mv folder/file.txt folder/rename.txt',))` and i'm getting FileNotFoundError, any idea? – ambigus9 Aug 02 '18 at 13:52
  • 1
    @Ambigus9 You need to put each part of the command in a separate tuple element: `subprocess.call(('cp', 'file.txt', 'folder'));` and then run it again for the `mv` command... – gahooa Aug 02 '18 at 20:30