[Tutor] No file or directory error using subprocess and Popen

Jim jf_byrnes at comcast.net
Mon May 15 22:17:03 EDT 2017


On 05/15/2017 08:33 PM, Jim wrote:
> On 05/15/2017 02:48 AM, Steven D'Aprano wrote:
>> On Sun, May 14, 2017 at 10:57:57PM -0500, Jim wrote:
>>> I am running this on Mint 18. This is the third script I have
>>> written to open and position windows in workspaces. The first two
>>> work, but trying to open ebook-viewe r (calibre) with a specific
>>> book produces the following error. If I run the same command in the
>>> terminal it works without an error.
>>
>> I think your problem is that you're telling subprocess to run a
>> command called:
>>
>> ebook-viewer
>> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub
>>
>>
>>  with no arguments. What you want is a command called:
>>
>> ebook-viewer
>>
>> and a single argument:
>>
>> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub
>>
>>
>>  I think (but haven't tried it) that the simplest way to fix that is
>> to change the entry in self.programs from:
>>
>>> self.programs = ['jedit', 'google-chrome', 'doublecmd',
>>> 'ebook-viewer
>>> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub']
>>>
>>
>>>
>> to:
>>
>> path_to_file =
>> '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'
>>
>>
>
>>
> self.programs = ['jedit',
>> 'google-chrome', 'doublecmd', ['ebook-viewer', path_to_file], ]
>
> I made the changes you suggested.
>
>     def __init__(self):
>         path_to_book =
> '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'
>
>         self.programs = ['jedit', 'google-chrome', 'doublecmd',
> ['ebook-viewer', path_to_book ]]
>         self.classname = {'jedit' : 'sun-awt-X11-XFramePeer',
>                             'google-chrome':'google-chrome',
>                             'doublecmd':'doublecmd',
>                             'calibre-ebook-viewer': 'libprs500'}
>         self.open_and_move()
>
> I noticed you have a , between the last two ]],s. I don't think you
> meant that but I tried it both ways just incase.
>
>>
>> and see if that fixes it. (It may not be enough, or the right
>> approach, but at least you'll get a different error if it is wrong
>> :-)
>
> Unfortunately you are correct, I did get a different error message.
>
> Exception in thread Thread-4:
> Traceback (most recent call last):
>   File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
>     self.run()
>   File "/usr/lib/python3.5/threading.py", line 862, in run
>     self._target(*self._args, **self._kwargs)
>   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in
> open_it
>     subprocess.call([self.program])
>   File "/usr/lib/python3.5/subprocess.py", line 557, in call
>     with Popen(*popenargs, **kwargs) as p:
>   File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
>     restore_signals, start_new_session)
>   File "/usr/lib/python3.5/subprocess.py", line 1474, in _execute_child
>     executable = os.fsencode(executable)
>   File "/usr/lib/python3.5/os.py", line 862, in fsencode
>     raise TypeError("expect bytes or str, not %s" %
> type(filename).__name__)
> TypeError: expect bytes or str, not list
>
> Traceback (most recent call last):
>   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 78, in
> <module>
>     Place()
>   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 21, in
> __init__
>     self.open_and_move()
>   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 31, in
> open_and_move
>     p = Popen(['xdotool', 'search', '--classname',
> self.classname[self.program]], stdout=subprocess.PIPE)
> TypeError: unhashable type: 'list'
>
> Regards,  Jim
>

Replying to myself to report that I got it working.
I changed open_it to:

     def open_it(self):
         if self.program == 'ebook-viewer':
             subprocess.call([self.program, self.path_to_book])
         else:
             subprocess.call([self.program])

After looking at it, I realized I should be providing the path when I 
was actually opening the file in open_it not further down in 
open_and_move where I was positioning the windows. Also I realize 
open_and_move is a poor name and needs to be changed to move_it or 
something.

I still get the following error but it does not seem to effect the 
programs operation. So for now I am just glad it is working.

Traceback (most recent call last):
   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 84, in 
<module>
     Place()
   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 21, in 
__init__
     self.open_and_move()
   File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 37, in 
open_and_move
     p = Popen(['xdotool', 'search', '--classname', 
self.classname[self.program]], stdout=subprocess.PIPE)
KeyError: 'ebook-viewer'

Thanks for everyones help,  Jim



More information about the Tutor mailing list