[Tutor] getting input for stdin
Alan Gauld
alan.gauld at btinternet.com
Mon Dec 8 09:15:28 CET 2014
Forwarding tom list
Please always use Reply All (or Reply List) to include the list.
On 08/12/14 03:50, diliup gabadamudalige wrote:
> I managed to get the output file and input file working.
> The output works fine but the input does not. It simply exits the
> program. No crash. Exits.It DOES NOT WAIT FOR ME TO TYPE IN THE
> inputfile.txt
>
> here is the program in all.
>
> I have attached the whole program if you think that will help.
> if __name__ == '__main__':
> ## menna me kaalla to change the output path
> p = os.getcwd()
> outfile = "\output_file.txt"
> infile = "\input_file.txt"
>
> outfilepath = p + outfile
> infilepath = p + infile
>
> sys.stdout = open(outfilepath, "w+")
> sys.stdin = open(infilepath, "w+")
>
> os.startfile(outfilepath)
> os.startfile(infilepath)
> ##--------------------------------------------
>
> for i in data.info <http://data.info>: # print all the scale info
> to window
>
> print i
>
> run = True
>
> while run:
> scaletemplate = ["C", "D", "E", "F", "G", "A", "B"]
> ## menna input ganna thana
> getscale = sys.stdin.readline()
> raw_input("Which scale do you require?:")
> ##---------------------------------------
> print getscale
>
>
>
> if len(getscale) > 1:
> getscale = getscale[0].upper() + getscale[1:]
> else:
> getscale = getscale.upper()
>
>
>
> if getscale in data.scalenames:
>
> scale = main(getscale)
>
> print scale
>
> elif getscale == "Q" or getscale == "X" or getscale == "":
> run = False
> print"exiting..."
>
> else:
> print "No such scale"
>
> On Mon, Dec 8, 2014 at 8:49 AM, diliup gabadamudalige
> <diliupg at gmail.com <mailto:diliupg at gmail.com>> wrote:
>
> I do not need to save any info in the text files opened for stdout
> and stdin.
> The text files are empty files used only for Python output or Input.
> I only need to use them as standard output and input.
> I do not need to both above in the same file.
> They may be in two different files.
> after the user gets his answers from the program, on exit the
> files may be closed without saving.
>
> Sorry for not adding the above in the previous email.
> Thank you again.
>
> On Mon, Dec 8, 2014 at 8:41 AM, diliup gabadamudalige
> <diliupg at gmail.com <mailto:diliupg at gmail.com>> wrote:
>
> Dear Allen,
>
> :)
>
> Thank you very much for the responses but some things have
> been totally misunderstood here. :)
>
> 1.What I want to do is to redirect the output of the program
> to a text file instead of the standard Python output. That is
> why I opened the text file. That part of the program works fine.
>
> 2. data is a py file named data.py which holds all the text I
> need to be printed to the console. As it is an explanation of
> music theory and will be large i have put it into a separate
> file which is loaded as a module at the begining of the
> program. hence data.info <http://data.info> which is a list of
> strings which are printed to the scree. Hence in my program I
> open a text file and send the print out put to that which is
> then printed to the text file. This works too.
>
> 3. When I ask for input in python the prompt which is usually
> in the Python output console waits for the user input and
> returns that as a string.
> 4.Insted of 3 above I would like the user to be able to type
> into a text file which may be opened by the program to collect
> input. For instance i open a text file named
> give_me_your_input.txt and then the user types his requirement
> in that text file which is taken as a string by the program
> which in turn either returns the appropriate answer.
>
> I know how to do everything else above except how to get the
> input from the text file in real time.
>
> My program works without any flaws without any of the stdin or
> stdout in the normal console.
>
> I hope this is clear enough to supply me with an answer.
>
> I thank you once again for your time and hope you can spare a
> little more to help me on the way.
>
>
> On Mon, Dec 8, 2014 at 7:33 AM, Alan Gauld
> <alan.gauld at btinternet.com <mailto:alan.gauld at btinternet.com>>
> wrote:
>
> 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>
> <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
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
> **********************************************************************************************
> This e-mail is confidential. It may also be legally
> privileged. If you are not the intended recipient or have
> received it in error, please delete it and all copies from
> your system and notify the sender immediately by return
> e-mail. Any unauthorized reading, reproducing, printing or
> further dissemination of this e-mail or its contents is
> strictly prohibited and may be unlawful. Internet
> communications cannot be guaranteed to be timely, secure,
> error or virus-free. The sender does not accept liability for
> any errors or omissions.
> **********************************************************************************************
>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
> **********************************************************************************************
> This e-mail is confidential. It may also be legally privileged. If
> you are not the intended recipient or have received it in error,
> please delete it and all copies from your system and notify the
> sender immediately by return e-mail. Any unauthorized reading,
> reproducing, printing or further dissemination of this e-mail or
> its contents is strictly prohibited and may be unlawful. Internet
> communications cannot be guaranteed to be timely, secure, error or
> virus-free. The sender does not accept liability for any errors or
> omissions.
> **********************************************************************************************
>
>
>
>
> --
> Diliup Gabadamudalige
>
> http://www.diliupg.com
> http://soft.diliupg.com/
>
> **********************************************************************************************
> This e-mail is confidential. It may also be legally privileged. If you
> are not the intended recipient or have received it in error, please
> delete it and all copies from your system and notify the sender
> immediately by return e-mail. Any unauthorized reading, reproducing,
> printing or further dissemination of this e-mail or its contents is
> strictly prohibited and may be unlawful. Internet communications
> cannot be guaranteed to be timely, secure, error or virus-free. The
> sender does not accept liability for any errors or omissions.
> **********************************************************************************************
>
More information about the Tutor
mailing list