473,378 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Input from the same file as the script

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 '06 #1
5 1440
po**************@gmail.com 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
Aug 20 '06 #2

you can (ab)use doc strings. See the doctest module for an example.
Aug 21 '06 #3

"Dennis Lee Bieber" <wl*****@ix.netcom.comWrote:
| On 20 Aug 2006 11:02:25 -0700, po**************@gmail.com 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

-

Aug 21 '06 #4
ZeD
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
Aug 21 '06 #5
Georg Brandl <g.*************@gmx.netwrites on Sun, 20 Aug 2006 20:08:38 +0200:
po**************@gmail.com 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
Aug 22 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gregory Lielens | last post by:
Hello all, I am not sure this is the correct list to ask as my problem is really related to python low level implementation, but as I do not have found a python.dev list, here it is :-) We...
2
by: J.R | last post by:
Greetings, I'm adding dynamically created input type='file' controls via JavaScript. However when I try to access they do not seem to be returned in the form collection. Any ideas? Thanks,...
2
by: Quick Function | last post by:
For the <input type="file">, can I select directory? Multiple directories? Thanks, qq
3
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink' href='javascript:AddFileInput()">Attach More Files </a> <input...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
9
by: paczkow | last post by:
Dear Python Community, I am an engineering and I am experiencing some trouble. Having output data from other software I want to use it. To achieve this I decided to use Python since this...
1
by: Rob Mitchell | last post by:
(Sorry if this appears twice) Hi, I know this has probably been beaten to death but I was working with Class by Stickman -- http://www.the-stickman.com which uses DOM to create multiple file...
12
by: Larry Bud | last post by:
I rarely crosspost, but this affects both ASP and Javascript REALLY odd bug that I ran across in ASP 3.0. I have an input type of file, user clicks browse, then places his cursor in the...
2
by: Ed Jay | last post by:
I'm dynamically creating several form input elements: mValue = integer constant; for(var j = 0; j < mValue; j++) { target = "imgCn"+ j; eName = "myFile"; eName = eName+jj;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.