Input from the same file as the script

Discussion in 'Python' started by poggle.themammal@gmail.com, Aug 20, 2006.

  1. Guest

    Can the input to the python script be given from the same file as the
    script itself. e.g., when we execute a python script with the command
    'python <scriptName', can the input be given in someway ?

    When I ran the below the python interpreter gave an error.

    e.g.,
    scriptName:
    -----------
    x = input("The value of x is taken from the source code file itself as
    input is redirected to it")

    print x

    "Value intended for the variable x"
    , Aug 20, 2006
    #1
    1. Advertising

  2. Georg Brandl Guest

    wrote:
    > Can the input to the python script be given from the same file as the
    > script itself. e.g., when we execute a python script with the command
    > 'python <scriptName', can the input be given in someway ?
    >
    > When I ran the below the python interpreter gave an error.


    *sigh* Why do you think that we could guess what error this may be?

    In this case, it is likely a SyntaxError because you used input() instead
    of raw_input().

    > e.g.,
    > scriptName:
    > -----------
    > x = input("The value of x is taken from the source code file itself as
    > input is redirected to it")
    >
    > print x
    >
    > "Value intended for the variable x"
    >


    Georg
    Georg Brandl, Aug 20, 2006
    #2
    1. Advertising

  3. On 20 Aug 2006 11:02:25 -0700, declaimed the
    following in comp.lang.python:

    > Can the input to the python script be given from the same file as the
    > script itself. e.g., when we execute a python script with the command
    > 'python <scriptName', can the input be given in someway ?
    >

    Redirecting? Ugh...

    Off-hand, I'd say NO

    There is no way to tell the python interpreter where the program
    ends and the "run data" begins.
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Aug 20, 2006
    #3
  4. you can (ab)use doc strings. See the doctest module for an example.
    Bernhard Mulder, Aug 21, 2006
    #4
  5. "Dennis Lee Bieber" <> Wrote:


    | On 20 Aug 2006 11:02:25 -0700, declaimed the
    | following in comp.lang.python:
    |
    | > Can the input to the python script be given from the same file as the
    | > script itself. e.g., when we execute a python script with the command
    | > 'python <scriptName', can the input be given in someway ?
    | >
    | Redirecting? Ugh...
    |
    | Off-hand, I'd say NO
    |
    | There is no way to tell the python interpreter where the program
    | ends and the "run data" begins.

    You *could* jump through a hoop like this one:

    ListOfInput = ['first input', 'second input', .......'last input']

    and then read the elements of the list one by one...

    You will probably have to make the list global to get it to work...

    But I kind of agree with Dennis - I would not do it that way either - reading
    the inputs from the stdin console is easy enough, or if there are just a few of
    them, getting them as command line arguments is arguably even easier - and if
    its a whole bunch of stuff that is painstaking to type in every time - put it in
    a text file and read it line by line.

    - Hendrik

    -
    Hendrik van Rooyen, Aug 21, 2006
    #5
  6. ZeD Guest

    Dennis Lee Bieber wrote:

    >> Can the input to the python script be given from the same file as the
    >> script itself. e.g., when we execute a python script with the command
    >> 'python <scriptName', can the input be given in someway ?
    >>

    > Redirecting? Ugh...
    >
    > Off-hand, I'd say NO
    >
    > There is no way to tell the python interpreter where the program
    > ends and the "run data" begins.


    maybe, as an UGLY hack he coud use some the comments

    --
    Under construction
    ZeD, Aug 21, 2006
    #6
  7. Georg Brandl <> writes on Sun, 20 Aug 2006 20:08:38 +0200:
    > wrote:
    > > Can the input to the python script be given from the same file as the
    > > script itself. e.g., when we execute a python script with the command
    > > 'python <scriptName', can the input be given in someway ?
    > > When I ran the below the python interpreter gave an error.


    The easiest way would be:

    data = '''\
    here comes your data
    ...
    '''

    # and now you use it
    ... data ...

    # you can even wrap it into a file
    from StringIO import StringIO
    data_as_file = StringIO(data)
    ... data_as_file.readline() ...

    --
    Dieter
    Dieter Maurer, Aug 22, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krick
    Replies:
    1
    Views:
    1,453
    John C. Bollinger
    Aug 15, 2003
  2. Robocop
    Replies:
    5
    Views:
    311
    Robocop
    Oct 17, 2008
  3. Christopher Brewster
    Replies:
    5
    Views:
    339
    John Machin
    Nov 14, 2008
  4. Replies:
    2
    Views:
    147
    Sean O'Halpin
    Dec 17, 2008
  5. paviinelec
    Replies:
    0
    Views:
    291
    paviinelec
    Apr 3, 2013
Loading...

Share This Page