Variable passing to external program - How??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rigga

    Variable passing to external program - How??

    Hi,

    Ok first please bear with me as I am a total Python n00b..

    Can anyone explain why this does not like me using % FileLoc in the
    os.system call???

    #!/usr/bin/python
    import sys
    import os
    # Check that the folder is accessible and writeable
    FileLoc = os.path.exists( '/home/rigga')
    if (FileLoc):
    print "File location exists:"
    AccFlag = os.access('% FileLoc',os.R_O K | os.X_OK | os.W_OK)
    if (AccFlag):
    print "You have FULL access to the location"
    else:
    print "**Error - You do not have access to the location**"
    else:
    print "No files found exiting..."

    sys.exit()

    I have full access to the folder I am checking however it always returns no
    files found (FileLoc = 0) however if I specify the folder I want to test in
    the os.system call it works fine...

    Any help appreciated

    Cheerz

    Rigga

  • Alex Martelli

    #2
    Re: Variable passing to external program - How??

    Rigga wrote:
    [color=blue]
    > Hi,
    >
    > Ok first please bear with me as I am a total Python n00b..[/color]

    OK, but: the post's subject bears no relation to your code; and
    neither does your text -- you keep talking about an os.system
    call that just isn't there. So, "noob" or not, I'm nonplussed.
    [color=blue]
    > Can anyone explain why this does not like me using % FileLoc in the
    > os.system call???[/color]

    There is no os.system call in the following code.
    [color=blue]
    > #!/usr/bin/python
    > import sys
    > import os
    > # Check that the folder is accessible and writeable
    > FileLoc = os.path.exists( '/home/rigga')
    > if (FileLoc):
    > print "File location exists:"
    > AccFlag = os.access('% FileLoc',os.R_O K | os.X_OK | os.W_OK)[/color]

    You're checking for a file called '% FileLoc', which does not exist. The
    variable FileLoc at this point is worth True, so you can't possibly want
    to "pass it to an external program" as per subject, either.
    [color=blue]
    > I have full access to the folder I am checking however it always returns
    > no files found (FileLoc = 0) however if I specify the folder I want to
    > test in the os.system call it works fine...[/color]

    There is no os.system call anywhere in the above.


    Alex

    Comment

    • Diez B. Roggisch

      #3
      Re: Variable passing to external program - How??

      > FileLoc = os.path.exists( '/home/rigga')

      This yields true or false, depending on the existence of "/home/rigga"
      I'm guessing now, but what you want is this:

      FileLoc = "/home/rigga"
      if os.path.exists( FileLoc):
      ....
      [color=blue]
      > if (FileLoc):
      > print "File location exists:"
      > AccFlag = os.access('% FileLoc',os.R_O K | os.X_OK | os.W_OK)[/color]

      Guessing again - you want to check your perms on that FileLoc of yours - so
      why don't you just pass it into the function?
      AccFlag = os.access(FileL oc,os.R_OK | os.X_OK | os.W_OK)

      The % operator works similar to printf in C/PHP/Whatever. If you absolutely
      want it here, this would work:
      [color=blue]
      >print "%s" % FileLoc[/color]
      "/home/rigga"


      --
      Diez

      Comment

      • Lee Harr

        #4
        Re: Variable passing to external program - How??

        > #!/usr/bin/python[color=blue]
        > import sys
        > import os
        > # Check that the folder is accessible and writeable
        > FileLoc = os.path.exists( '/home/rigga')
        > if (FileLoc):
        > print "File location exists:"
        > AccFlag = os.access('% FileLoc',os.R_O K | os.X_OK | os.W_OK)
        > if (AccFlag):
        > print "You have FULL access to the location"
        > else:
        > print "**Error - You do not have access to the location**"
        > else:
        > print "No files found exiting..."
        >
        > sys.exit()
        >[/color]

        import os
        FileLoc = '/home/rigga'

        if os.path.exists( FileLoc):
        print "File location exists"
        AccFlag = os.access(FileL oc, os.R_OK | os.X_OK | os.W_OK)
        if AccFlag:
        print "You have FULL access to the location"
        else:
        print "**Error - You do not have access to the location**"
        else:
        print "No files found exiting..."

        Comment

        • Rigga

          #5
          Re: Variable passing to external program - How??

          Alex Martelli wrote:
          [color=blue]
          > Rigga wrote:
          >[color=green]
          >> Hi,
          >>
          >> Ok first please bear with me as I am a total Python n00b..[/color]
          >
          > OK, but: the post's subject bears no relation to your code; and
          > neither does your text -- you keep talking about an os.system
          > call that just isn't there. So, "noob" or not, I'm nonplussed.
          >[color=green]
          >> Can anyone explain why this does not like me using % FileLoc in the
          >> os.system call???[/color]
          >
          > There is no os.system call in the following code.
          >[color=green]
          >> #!/usr/bin/python
          >> import sys
          >> import os
          >> # Check that the folder is accessible and writeable
          >> FileLoc = os.path.exists( '/home/rigga')
          >> if (FileLoc):
          >> print "File location exists:"
          >> AccFlag = os.access('% FileLoc',os.R_O K | os.X_OK | os.W_OK)[/color]
          >
          > You're checking for a file called '% FileLoc', which does not exist. The
          > variable FileLoc at this point is worth True, so you can't possibly want
          > to "pass it to an external program" as per subject, either.
          >[color=green]
          >> I have full access to the folder I am checking however it always returns
          >> no files found (FileLoc = 0) however if I specify the folder I want to
          >> test in the os.system call it works fine...[/color]
          >
          > There is no os.system call anywhere in the above.
          >
          >
          > Alex[/color]
          Alex, point taken I rushed when I was doing this and made many mistakes,
          thatll learn me not to rush a post!.

          To all the others that replied thanks for your input it has helped me a lot.

          What I was going to add to the original post but forgot was how you pass
          variables to external programs - hence the title. I have been reading
          though the Python Bible however all this variable passing doesnt appear to
          work as I would expect i.e.

          FilePath = os.path('/home/rigga')
          AccFlag = os.access('% FilePath',os.R_ OK)

          I would expect % FilePath to contain /home/rigga and for os.access to parse
          % FilePath in to that and return the results.... however it doesnt, it only
          works if I specify the directory in os.access...

          Or am I just being stupid?

          Cheerz

          Rigga

          Comment

          • Christopher Koppler

            #6
            Re: Variable passing to external program - How??

            On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Rigga@noemail. com> wrote:
            [color=blue]
            >FilePath = os.path('/home/rigga')
            >AccFlag = os.access('% FilePath',os.R_ OK)
            >
            >I would expect % FilePath to contain /home/rigga[/color]

            Why would you expect that?


            --
            Christopher

            Comment

            • Rigga

              #7
              Re: Variable passing to external program - How??

              Christopher Koppler wrote:
              [color=blue]
              > On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Rigga@noemail. com> wrote:
              >[color=green]
              >>FilePath = os.path('/home/rigga')
              >>AccFlag = os.access('% FilePath',os.R_ OK)
              >>
              >>I would expect % FilePath to contain /home/rigga[/color]
              >
              > Why would you expect that?
              >
              >
              > --
              > Christopher[/color]
              because Ive assigned it using the FilePath = os.path('/home/rigga') -
              surely therefore FilePath contains the value /home/rigga???????

              Comment

              • Christopher Koppler

                #8
                Re: Variable passing to external program - How??

                On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Rigga@noemail. com> wrote:
                [color=blue]
                >Christopher Koppler wrote:
                >[color=green]
                >> On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Rigga@noemail. com> wrote:
                >>[color=darkred]
                >>>FilePath = os.path('/home/rigga')
                >>>AccFlag = os.access('% FilePath',os.R_ OK)
                >>>
                >>>I would expect % FilePath to contain /home/rigga[/color]
                >>
                >> Why would you expect that?[/color]
                >
                >because Ive assigned it using the FilePath = os.path('/home/rigga') -
                >surely therefore FilePath contains the value /home/rigga???????[/color]

                Yes, but what do you think that '% FilePath' means? This is an
                ordinary string, and does not magically expand to the variable's
                contents, which I assume you wanted. You need to use just the variable
                name for that:

                AccFlag = os.access(FileP ath, os.R_OK)

                If you wanted to use the % operator for strings, you could also write
                that as

                AccFlag = os.access('%s' % FilePath, os.R_OK)

                which is completely unnecessary in this case, however.

                --
                Christopher

                Comment

                • Christopher Koppler

                  #9
                  Re: Variable passing to external program - How??

                  On Sat, 11 Oct 2003 15:22:36 GMT, Christopher Koppler
                  <klapotec@chell o.at> wrote:
                  [color=blue]
                  >On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Rigga@noemail. com> wrote:
                  >[color=green]
                  >>Christopher Koppler wrote:
                  >>[color=darkred]
                  >>> On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Rigga@noemail. com> wrote:
                  >>>
                  >>>>FilePath = os.path('/home/rigga')
                  >>>>AccFlag = os.access('% FilePath',os.R_ OK)
                  >>>>
                  >>>>I would expect % FilePath to contain /home/rigga
                  >>>
                  >>> Why would you expect that?[/color]
                  >>
                  >>because Ive assigned it using the FilePath = os.path('/home/rigga') -
                  >>surely therefore FilePath contains the value /home/rigga???????[/color]
                  >
                  >Yes, but what do you think that '% FilePath' means? This is an
                  >ordinary string, and does not magically expand to the variable's
                  >contents, which I assume you wanted. You need to use just the variable
                  >name for that:
                  >
                  >AccFlag = os.access(FileP ath, os.R_OK)
                  >
                  >If you wanted to use the % operator for strings, you could also write
                  >that as
                  >
                  >AccFlag = os.access('%s' % FilePath, os.R_OK)
                  >
                  >which is completely unnecessary in this case, however.[/color]

                  And also, I completely overlooked:
                  FilePath = os.path('/home/rigga') will not work either, because
                  os.path is a _module_ (which is not callable), not a function to
                  create paths. Pathnames are just strings, so

                  FilePath = '/home/rigga'

                  is what you want.


                  --
                  Christopher

                  Comment

                  • Cousin Stanley

                    #10
                    Re: Variable passing to external program - How??

                    Following is yet another version for checking file access
                    that takes the file_path as an argument to the module,
                    and differentiates R,E,W or NO access ....

                    '''
                    Module ....... os_file_access. py
                    NewsGroup .... comp.lang.pytho n
                    Date ......... 2003-10-10
                    Posted_By .... rigga
                    Edited_By .... Stanley C. Kitching
                    '''

                    import os
                    import sys

                    NL = '\n'
                    SP = ' '
                    SP3 = SP * 3
                    SP7 = SP * 7

                    module_this = sys.argv[ 0 ]

                    print '%s %s ' % ( NL , module_this ) , NL

                    if len( sys.argv ) < 2 :

                    print SP7 , 'Usage : python os_file_access. py file_path' , NL

                    sys.exit( -1 )

                    file_path = sys.argv[ 1 ] # Input File Path as 1st Argument

                    if os.path.exists( file_path ) :

                    print SP7 , "File Path Exists ...." , file_path , NL

                    read_flag = os.access( file_path , os.R_OK )
                    exec_flag = os.access( file_path , os.X_OK )
                    write_flag = os.access( file_path , os.W_OK )

                    else :

                    print SP7 , "*** File NOT Found ***" , NL
                    print SP7 , SP3 , file_path , NL

                    sys.exit( -2 )

                    list_access = []

                    if read_flag : list_access.app end( 'Read' )
                    if exec_flag : list_access.app end( 'Execute' )
                    if write_flag : list_access.app end( 'Write' )

                    if ( read_flag | exec_flag | write_flag ) :

                    str_access = ' , '.join( list_access )

                    else :

                    str_access = 'NO'

                    print SP7 , 'You have [ %s ] Access to the File' % ( str_access ) , NL

                    --
                    Cousin Stanley
                    Human Being
                    Phoenix, Arizona

                    Comment

                    • Rigga

                      #11
                      Re: Variable passing to external program - How??

                      Christopher Koppler wrote:
                      [color=blue]
                      > On Sat, 11 Oct 2003 15:22:36 GMT, Christopher Koppler
                      > <klapotec@chell o.at> wrote:
                      >[color=green]
                      >>On Sat, 11 Oct 2003 16:08:46 +0000, Rigga <Rigga@noemail. com> wrote:
                      >>[color=darkred]
                      >>>Christophe r Koppler wrote:
                      >>>
                      >>>> On Sat, 11 Oct 2003 15:50:29 +0000, Rigga <Rigga@noemail. com> wrote:
                      >>>>
                      >>>>>FilePath = os.path('/home/rigga')
                      >>>>>AccFlag = os.access('% FilePath',os.R_ OK)
                      >>>>>
                      >>>>>I would expect % FilePath to contain /home/rigga
                      >>>>
                      >>>> Why would you expect that?
                      >>>
                      >>>because Ive assigned it using the FilePath = os.path('/home/rigga') -
                      >>>surely therefore FilePath contains the value /home/rigga???????[/color]
                      >>
                      >>Yes, but what do you think that '% FilePath' means? This is an
                      >>ordinary string, and does not magically expand to the variable's
                      >>contents, which I assume you wanted. You need to use just the variable
                      >>name for that:
                      >>
                      >>AccFlag = os.access(FileP ath, os.R_OK)
                      >>
                      >>If you wanted to use the % operator for strings, you could also write
                      >>that as
                      >>
                      >>AccFlag = os.access('%s' % FilePath, os.R_OK)
                      >>
                      >>which is completely unnecessary in this case, however.[/color]
                      >
                      > And also, I completely overlooked:
                      > FilePath = os.path('/home/rigga') will not work either, because
                      > os.path is a _module_ (which is not callable), not a function to
                      > create paths. Pathnames are just strings, so
                      >
                      > FilePath = '/home/rigga'
                      >
                      > is what you want.
                      >
                      >
                      > --
                      > Christopher[/color]
                      Thank you!! it makes more sense to me now.

                      Cheers

                      Rigga

                      Comment

                      Working...