[Tutor] Path?
Jim Byrnes
jf_byrnes at comcast.net
Thu Jul 15 18:21:26 CEST 2010
Adam Bark wrote:
> On 14 July 2010 17:41, Jim Byrnes<jf_byrnes at comcast.net> wrote:
>
>> Adam Bark wrote:
>>
>>> On 14 July 2010 02:53, Jim Byrnes<jf_byrnes at comcast.net> wrote:
>>>
>>> Adam Bark wrote:
>>>>
>>>> <snipped some old stuff>
>>>>
>>>>
>>>> If I use the terminal to start the program it has no problem using the
>>>>
>>>>> file. There are multiple files in multiple directories so I was
>>>>>>>> looking
>>>>>>>> for
>>>>>>>> a way to just double click them and have them run. If it turns out
>>>>>>>> that
>>>>>>>> I
>>>>>>>> must make changes to or for each of the files it will be easier to
>>>>>>>> just
>>>>>>>> keep
>>>>>>>> using the terminal. I've only been using Ubuntu for a few months so
>>>>>>>> I
>>>>>>>> was
>>>>>>>> surprised that the program could not see a file that is in the same
>>>>>>>> directory.
>>>>>>>>
>>>>>>>> Regards, Jim
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> The problem is ubuntu doesn't run the script from the directory it's
>>>>>>> in
>>>>>>> so
>>>>>>> it's looking for wxPython.jpg somewhere else.
>>>>>>>
>>>>>>>
>>>>>>> OK, I mistakenly thought that double-clicking on file in Nautilus
>>>>>>> would
>>>>>>>
>>>>>> take care of the path info.
>>>>>>
>>>>>> In my reply above I also mentioned that I tried by dropping it on a
>>>>>> Launcher on the top panel and that the command the launcher uses is
>>>>>> usr/bin/python2.6. Is there a way that the command can be changed so
>>>>>> that
>>>>>> it will look in the same directory the python script is in for any file
>>>>>> it
>>>>>> needs?
>>>>>>
>>>>>> Thanks, Jim
>>>>>>
>>>>>>
>>>>>
>>>>> Not sure if you got my previous email but you could try writing the bash
>>>>> script I posted (with the $1 line to get the path) and setting that as
>>>>> your
>>>>> launcher, I think it should work.
>>>>>
>>>>> Let me know if you didn't get it or it doesn't work.
>>>>>
>>>>> HTH,
>>>>> Adam.
>>>>>
>>>>>
>>>>> I got it, got sidetracked and then forgot to look at it again. Thanks
>>>> for
>>>> reminding me. Your idea works, but with one little downside. The
>>>> directories I am working with are chapters in a book. So as I move from
>>>> chapter to chapter I will need to change the bash script, but this seems
>>>> to
>>>> be less typing than using the terminal.
>>>>
>>>>
>>>> Thanks, Jim
>>>>
>>>>
>>> Ok cool, glad it works. It might be possible to get the path so you don't
>>> have to set it each time, try this:
>>>
>>> #!/bin/bash
>>> IFS="/"
>>> path=($1)
>>> cd $(path[0:#path[*]])
>>> python $1
>>>
>>>
>>> # Warning, I'm not exactly a competent bash programmer so this may not
>>> work
>>> :-p
>>>
>>> Let me know if you need a hand to fix it,
>>>
>>> HTH,
>>> Adam.
>>>
>>>
>> I tried the new bash code but when I dropped a file on the launcher it just
>> flashed an gave no output. So I tried running the bash script
>> (name=runpython) in a terminal and got this error:
>>
>> /home/jfb/runpython: line 4: path[0:#path[*]]: command not found
>> Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>
>>
>> I know even less about bash than you do, so I don't where to start to debug
>> this.
>>
>>
>> Thanks, Jim
>>
>> Ok then, this time it's tested and not just improvised, here we go:
>
> #!/bin/bash
>
> script=$1 # Full path for calling the script later
> orig_IFS=$IFS # This is to reset IFS so that "script" is correct (otherwise
> has spaces instead of /)
> IFS="/"
> path=( $1 )
> IFS=$orig_IFS
> last_ind=${#path[@]} # Works out the length of path
> let "last_ind -= 1" # Sets last_ind to index of script name
> len_path=${path[@]:0:last_ind} # Gets the path without the script name
> let "len_path=${#len_path[0]} + 1" # This gives the length of the script
> string upto just before the last /
> cd ${script[@]:0:len_path} # cds to the path
> python script
>
>
> As pretty much my first non-trivial bash script it's probably horrible but
> it seems to work.
>
> HTH,
> Adam.
>
There must be something different in our setups because it did not work
for me. If I run it from a terminal I get:
jfb at jfb-ubuntu64:~$ /home/jfb/runpython_test bitmap_button.py
/home/jfb/runpython_test: line 12: cd: b: No such file or directory
python: can't open file 'script': [Errno 2] No such file or directory
jfb at jfb-ubuntu64:~$
Thanks Jim
More information about the Tutor
mailing list