Syntax error on filename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joydeepdg
    New Member
    • Apr 2010
    • 9

    Syntax error on filename

    >>> python WavHeader.py hello.wav
    File "<stdin>", line 1
    python WavHeader.py hello.wav
    ^(this arrow mark is actually below 'r' of WavHeader.py)
    SyntaxError : invalid syntax
    Attached Files
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I tested your script from the Command Prompt without a hitch. At the python prompt, use the import statement. This worked for me:
    Code:
    >>> import WaveHeader
    >>> WaveHeader.PrintWavHeader("sample.wav")
    Subchunks Found:
    fmt ,  data,
    
    Data Chunk located at offset [36] of data length [15022108] bytes
    BitsPerSample:  16
    NumChannels:  2
    ChunkSize:  15022144
    Format:  WAVE
    Filename:  sample.wav
    ByteRate:  176400
    Subchunk1Size:  16
    AudioFormat:  1
    BlockAlign:  4
    SampleRate:  44100
    >>>

    Comment

    • joydeepdg
      New Member
      • Apr 2010
      • 9

      #3
      i'm still facing the same problem. i'm new to this python thing. is thr something wrong with some other configuration? as in computer's? or what?
      and in the IDLE when i'm doing with the import..
      this is what i get:
      >>> import WavHeader
      Traceback (most recent call last):
      File "<pyshell#0 >", line 1, in <module>
      import WavHeader
      ImportError: No module named WavHeader

      >>> WavHeader.Print WavHeader("hell o.wav")
      Traceback (most recent call last):
      File "<pyshell#1 >", line 1, in <module>
      WavHeader.Print WavHeader("hell o.wav")
      NameError: name 'WavHeader' is not defined


      reply from your end is eagerly awaited.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        In IDLE, import the os module and set the current working directory to the directory containing WaveHeader.py. If the WAV file is in another directory, use the full path in the argument.

        Code:
        import os
        os.chdir("XX:/directory_name")

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          An alternative: import sys and append the path to the directory to sys.path.
          Code:
          import sys
          sys.path.append("XX:/directory_name")
          You may have to create a file named "__init__.p y" and place it in the same directory as WaveHeader.py. It need not have anything in it.

          Comment

          • joydeepdg
            New Member
            • Apr 2010
            • 9

            #6
            I am facing some wierd problems here.I deleted the first few lines of the code from the IDLE including the ">>>" sign till so that the first line read
            "# WavHeader.py
            #Extract .....
            "
            Now I start the python command line and at the >>> promt I give the following command:

            >>> import os
            >>> os.system("pyth on WavHeader.py hello.wav")
            Now the earlier error,i.e. the syntax error disappears but a new error appears as:

            File "WavHeader. py", line 17
            print "%s:" % (key), structHeaderFie lds[key]

            Now I really dont know why I am having all these errors and amreally feeling low because I was expecting the code to run without a hitch as you did.But still I am stuck.
            Please help me out with this one.
            Awaiting your response.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Open the file in Idle. This sets the current working directory to the location of the module. At the Python prompt of the shell window:
              Code:
              >>> import WaveHeader
              >>> WaveHeader.PrintWavHeader("sample.wav")
              Subchunks Found: 
              fmt ,  data,  
              
              Data Chunk located at offset [36] of data length [15022108] bytes
              BitsPerSample:  16
              NumChannels:  2
              ChunkSize:  15022144
              Format:  WAVE
              Filename:  sample.wav
              ByteRate:  176400
              Subchunk1Size:  16
              AudioFormat:  1
              BlockAlign:  4
              SampleRate:  44100
              >>>
              Keep trying, you are bound to hit it eventually. :)

              Comment

              • joydeepdg
                New Member
                • Apr 2010
                • 9

                #8
                i do as above and i get the following:

                >>> import WavHeader
                Traceback (most recent call last):
                File "<pyshell#0 >", line 1, in <module>
                import WavHeader
                File "E:\Python31\Wa vHeader.py", line 17
                print "%s: " % (key), structHeaderFie lds[key]
                ^
                SyntaxError: invalid syntax

                Comment

                • joydeepdg
                  New Member
                  • Apr 2010
                  • 9

                  #9
                  ^ is below the second "

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    What version of Python are you using? If it's 3.0 or 3.1, print is a function whereas it was a statement in 2.X.

                    Comment

                    • joydeepdg
                      New Member
                      • Apr 2010
                      • 9

                      #11
                      it's Python 3.1

                      help me out, bro.

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by joydeepdg
                        it's Python 3.1

                        help me out, bro.
                        You can find the proper usage for the built-in function print() here.

                        Comment

                        • joydeepdg
                          New Member
                          • Apr 2010
                          • 9

                          #13
                          which version of python are u using?

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            I am using 2.3 because that is the version embedded in the software I use for my work. I have 2.6 installed when I want to try something new.

                            Comment

                            • Domi
                              New Member
                              • Apr 2010
                              • 2

                              #15
                              I'm running into this exact same error and I can't for the life of me figure out what is going wrong. I'm doing the Python tutorials made by google

                              Using the following code

                              Code:
                              #!/usr/bin/python -tt
                              # Copyright 2010 Google Inc.
                              # Licensed under the Apache License, Version 2.0
                              # http://www.apache.org/licenses/LICENSE-2.0
                              
                              # Google's Python Class
                              # http://code.google.com/edu/languages/google-python-class/
                              
                              """A tiny Python program to check that Python is working.
                              Try running this program from the command line like this:
                                python hello.py
                                python hello.py Alice
                              That should print:
                                Hello World -or- Hello Alice
                              Try changing the 'Hello' to 'Howdy' and run again.
                              Once you have that working, you're ready for class -- you can edit
                              and run Python code; now you just need to learn Python!
                              """
                              
                              import sys
                              
                              # Define a main() function that prints a little greeting.
                              def main():
                                # Get the name from the command line, using 'World' as a fallback.
                                if len(sys.argv) >= 2:
                                  name = sys.argv[1]
                                else:
                                  name = 'World'
                                print 'Hello', name
                              
                              # This is the standard boilerplate that calls the main() function.
                              if __name__ == '__main__':
                                main()

                              The most irritating thing is, using the shell, I can open the module and run it (f5) and it prints Hello World, but every time I navigate over to the directory and try running it through the command line interface (import hello, python hello.py) it keeps giving me this error. Sorry to the OP about pseudo hijacking your thread here :P but maybe this might shed more light on the issue. I'm using python 2.6

                              Comment

                              Working...