Results 1 to 6 of 6

Thread: continuation in a for loop

  1. #1
    Join Date
    Jan 2012
    Beans
    160

    continuation in a for loop

    First off this code is rough, hasnt been even cleaned up yet, but the question is still clear:

    Code:
    import sys
    import os.path
    import subprocess
    import time
    
    def main():
      if len(sys.argv) == 1:
        subprocess.call(['espeak', 'i need a directory please'])
        sys.exit(0)
      elif len(sys.argv) == 2:
        if os.path.isdir(sys.argv[1]):
          a = os.listdir(sys.argv[1])
          subprocess.call(['espeak', 'there are' + str(len(a)) + ' items in the directory' + str(sys.argv[1])])
          time.sleep(1)
          subprocess.call(['espeak','they break down as follows'])
          for item in a:
            b = item.split('.')
            subprocess.call(['espeak', b[0]]);
            os.path.isdir(item)
        else:       
          subprocess.call(['espeak', sys.argv[1] + 'is not a valid directory'])
      else:
        subprocess.call(['espeak', 'too many arguments.  exiting'])
    
    
    
    if __name__ == '__main__':
      main()
    The underlined segment of code is not executing. Is it something with the subprocess.call before it? or is there something i should do? I even put a semicolon in there to see...

  2. #2
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: continuation in a for loop

    Quote Originally Posted by wingnut2626 View Post
    The underlined segment of code is not executing.
    How do you know? os.path.isdir won't give you any output unless you're testing your code interactively in Python interpreter.

  3. #3
    Join Date
    Jan 2012
    Beans
    160

    Re: continuation in a for loop

    Well the original segment of code was:
    if os.path.isdir(item): subprocess.call(['espeak' , 'spam']) #was not getting desired output

  4. #4
    Join Date
    Jan 2012
    Beans
    160

    Re: continuation in a for loop

    Code:
    import sys
    import os.path
    import subprocess
    import time
    
    def main():
      if len(sys.argv) == 1:
        subprocess.call(['espeak', 'i need a directory please'])
        sys.exit(0)
      elif len(sys.argv) == 2:
        if os.path.isdir(sys.argv[1]):
          a = os.listdir(sys.argv[1])
          subprocess.call(['espeak', 'there are' + str(len(a)) + ' items in the directory' + str(sys.argv[1])])
          time.sleep(1)
          subprocess.call(['espeak','they break down as follows'])
          for item in a:
            b = item.split('.')
            subprocess.call(['espeak', b[0]]);
            print(os.path.abspath(item))
            if os.path.isdir(item)):
              print(item)
            else:
              print('Nope')
            input()
        else:       
          subprocess.call(['espeak', sys.argv[1] + 'is not a valid directory'])
      else:
        subprocess.call(['espeak', 'too many arguments.  exiting'])
    
    
    
    if __name__ == '__main__':
      main()
    Added the underlined code (for debugging purposes only). Shows the directory path on the printout, but everything gets a 'Nope'. I did notice that because of the split('.') i was losing readout of the hidden directories and files, but that has nothing to do with the representation of item in the underlined code.....stumped....

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,443
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: continuation in a for loop

    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,116
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: continuation in a for loop

    You are confused about your various paths and directories:

    • 'os.listdir(sys.argv[1])' lists the names of files/directories in that directory (but not their full path)
    • 'os.path.abspath(item)' understands 'item' (which has no path) as the name of a file in your current directory (not sys.argv[1]) and so builds a full path to that hypothetical file (that doesn't exist) (this function does more or less does the same thing as the 'readlink' command)
    • there is not much point into printing the output of 'os.path.abspath(item)' since you aren't using it in the code. But it shouldn't be printing the file paths you think and this should be a clue.




Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •