[Tutor] getting input for stdin

Alan Gauld alan.gauld at btinternet.com
Mon Dec 8 03:03:16 CET 2014

On 07/12/14 17:38, diliup gabadamudalige wrote:

> if __name__ == '__main__':

You don't really need that unless your code can be treated
as a module, which yours can't.

>      p = os.getcwd()
>      filename = "\get scale of choice.txt"
>      filepath = p + filename
>      sys.stdout = open(filepath, "w")

Why are you overwriting stdout with a file?
Why not just write to the file directly?
Usually if you do overwrite stdout you make a reference
to the old stdout first so you can restore it later.

>      os.startfile(filepath)

This tries to execute filepath, but you just opened it
in write mode which creates an empty file. So you are
trying to execute an empty file?

>      for i in data.info <http://data.info>:

What is data?
and what is the url like thing supposed to be?

Have you done a tutorial on Python?
Do you understand how the for loop works?
It needs an iterator/collection to operate on.

# print all the scale info
> to window
>          print i

This will print to stdout, which you have assigned
to a file above. So it won't print in any window.

>      run = True
>      while run:
>          scaletemplate = ["C", "D", "E", "F", "G", "A", "B"]
>          getscale = sys.stdin.raw_input(filepath)

Not sure what this is doing but raw_input reads from
stdin - it is not a method of stdin. And the argument to stdin is 
supposed to be a prompt to the user, you have passed a filename?

>          #getscale = raw_input("Which scale do you require?:")
>          if len(getscale) > 1:
>              getscale = getscale[0].upper() + getscale[1:]

getscale is commented out so this will raise an error.

>          else:
>              getscale = getscale.upper()

>          if getscale in data.scalenames:
>              scale = main(getscale)
>              print scale

Again, what is data?

>          elif getscale == "Q" or getscale == "X" or getscale == "":
>              run = False
>              print"exiting..."
>          else:
>              print "No such scale"

Again, these prints will go to your file since it is stdout.

> I need to get the stdin input from the text I type into the same text
> file that I have stdout at.

How would that work exactly?
You want to open the file in a text editor or somesuch? Then as you type 
into it you want Python to read the values you type? Before you save it? 
Or after? And you also want the output from Python to go into the file 
that you are editing? While you are editing it?

Can you explain exactly how the user is expected to use this combination 
of things?

It is not clear, very unlike any normal computing task and probably 
impossible. I suspect you have a concept in your mind but it's not
what you are describing here.

> How do I do that. None of the answers at
> stackoverflow got me going.

I'm not surproised, I think what you are asking is impossible
(or at least very difficult) , and even if it isn't it would be
a weird way of working.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list