I have a directory on my server with loads of Oscam files in it.
I want a menu, which shows them, allows me to choose which one, then runs it.
This can apply to Linux boxes (I think) so should work on DM/VU etc??
That's the plan anyway.
I have a directory on my server with loads of Oscam files in it.
I want a menu, which shows them, allows me to choose which one, then runs it.
This can apply to Linux boxes (I think) so should work on DM/VU etc??
That's the plan anyway.
catoro (14-05-2012), Dig Deep (14-05-2012), Friendly-Face (15-05-2012), mikie8 (14-05-2012), saintomer1866 (14-05-2012)
this is as far as I got ...
there are quite a few 'checks' in this code; and you can change the directory it looks in.Code:#!/usr/bin/python import fnmatch import os from subprocess import call #oscamdir = '/nas/oscam/' oscamdir = 'C:/Users/andrew/Downloads' file_list = [] #for fileName in os.listdir ( '/nas/oscam/' ): for fileName in os.listdir ( oscamdir ): if fnmatch.fnmatch ( fileName, 'oscam*' ): file_list.append(fileName) print " ****** MENU ******" for i in file_list: print 1 + file_list.index(i), print ") " + i print "* ) to quit" choice = raw_input("Which item do you want? ") print choice n = int(choice)-1 print file_list[n] final = oscamdir+file_list[n]+ ' -b -c '+oscamdir+'config' print final #sudo /nas/oscam/oscam6510 -b -c /nas/oscam/config
if you want me to explain. line by line, I can
Last edited by oldfart; 14-05-2012 at 05:09 PM.
mikie8 (14-05-2012), saintomer1866 (14-05-2012)
Thanks Oldfart,
yes i think a line by line explanation would be useful to all us novices in order to try and start to understand commands and syntax
i would also be very interested in an explaination .
well ... I am NOT an expert in Python programming, but I'll give it a go.
Bear in mind, its HOT here at the moment (50C in the sun) and the pool is 32C, so spending a fair amount of time in the pool.
Anyone can help out!
Python code will have different start-up lines (dunno what they are called) depending what machine the program is run on.
the first line
#!/usr/bin/python
is for a linux pc
a DM program will have a different first line
When do you
#!/usr/local/bin/python
You are specifying the location to the python executable in your machine, that rest of the script needs to be interpreted with.
You are pointing to python is located at /usr/local/bin/python
Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail.
Last edited by oldfart; 14-05-2012 at 05:05 PM.
scrapman (15-05-2012)
the 'import' commands allow you to make calls to modules previously written (and in this case, part of Python)
so the lines
import fnmatch
import os
from subprocess import call
allow you to call os.something or fnmatch.something or use the call command.
The idea of the program is to list files in a directory that contain the name "oscam"
so, rather than write out the directory every time we use it, it is best to store the directory name in a variable.
does this. oscamdir is shown here with two options, one is my Linux PC, the other is the Windows PC I used for testing the code.Code:#oscamdir = '/nas/oscam/' oscamdir = 'C:/Users/andrew/Downloads'
note the '#' symbol - this is a comment and a line starting with a # is ignored when the program runs.
Last edited by oldfart; 14-05-2012 at 05:29 PM.
scrapman (15-05-2012)
this creates a 'list'Code:file_list = []
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.
****> a = ['spam', 'eggs', 100, 1234]
****> a
['spam', 'eggs', 100, 1234]
creates a loop (FOR .... NEXT) - except there is no NEXT in Python - the indents (TAB characters) indicate where the loop starts/endsCode:for fileName in os.listdir ( oscamdir ): if fnmatch.fnmatch ( fileName, 'oscam*' ): file_list.append(fileName)
the program goes through the directory listing
for fileName in os.listdir ( oscamdir ):
if the entry it found matches what we are looking for
if fnmatch.fnmatch ( fileName, 'oscam*' ):
it adds the name to the list
file_list.append(fileName)
Last edited by oldfart; 15-05-2012 at 10:43 AM.
Next, we create the menu:
this prints out each element of the list, together with a number - note that lists start at 0 - so we add 1 to the start of the list, so the numbering starts at 1.Code:print " ****** MENU ******" for i in file_list: print 1 + file_list.index(i), print ") " + i print "* ) to quit" choice = raw_input("Which item do you want? ")
then allow the user to choose which entry they want.
this lot of code was just so I knew what is going on:
Attachment 53978Code:print choice n = int(choice)-1 print file_list[n] final = oscamdir+file_list[n]+ ' -b -c '+oscamdir+'config' print final
Last edited by oldfart; 15-05-2012 at 03:57 PM.
haven't worked out how to actually run the command yet ....
Bookmarks