Input from the same file as the script

P

poggle.themammal

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"
 
G

Georg Brandl

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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
H

Hendrik van Rooyen

| On 20 Aug 2006 11:02:25 -0700, (e-mail address removed) 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

-
 
Z

ZeD

Dennis said:
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
 
D

Dieter Maurer

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() ...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top