The Artima Developer Community
Sponsored Link

Python Answers Forum
writing string variable to file

9 replies on 1 page. Most recent reply: Apr 1, 2019 3:29 PM by Gordon Blue

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
alvin murphy

Posts: 4
Nickname: amurphy
Registered: Oct, 2004

writing string variable to file Posted: Oct 19, 2004 4:45 PM
Reply to this message Reply
Advertisement
Hi: I am totally new, trying to learn. How do you write a string variable to a file? The following does not work, as you can see.

IDLE 1.0.2
>>> str_Name = "Doe-John"
>>> myfile = open("myfile", "w")
>>> myfile.write(str_Name)
>>> myfile.close
<built-in method close of file object at 0x00942360>
>>> myfile = open("myfile", "r")
>>> myfile.readline()
''
>>>

Thanks


Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: writing string variable to file Posted: Oct 20, 2004 2:00 AM
Reply to this message Reply
if you put a newline char on the end of the text;

myfile.write(str_name + '\n')

Then try again. I guess it's because readline expects a full line. Also call "myfile.close()" - myfile.close just shows you the .close method.

You can strip off the newline when you fetch the line back:

>>> myfile.readline()
'John Doe\n'
>>> _.rstrip() # ('_' is set to the last value)
'John Doe'

alvin murphy

Posts: 4
Nickname: amurphy
Registered: Oct, 2004

Re: writing string variable to file Posted: Oct 20, 2004 2:19 PM
Reply to this message Reply
Thanks for your help. I am a total beginner; a little visual basic years ago. Eventually want to write a history taking program for psychiatry. So these are baby steps. Your solution works, but not in my Win Idle editor; i.e. if I write the code there and ask it to run the module, it does not work (the file and readline part). It works if I type it in the interpreter GUI. Do not understand that. Maybe I am "running" it wrong, i.e. maybe choosing "run module" is the wrong option.

Also do not understand your last line of code that strips the trailing \n. Could you help with that. Also is there a read command that reads all the text in there, not just a line? Thanks

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: writing string variable to file Posted: Oct 21, 2004 2:20 AM
Reply to this message Reply
That's odd about the module not working. Post the relevant bits here for us to see.

As for the last bit of my code - Whenever you do something that returns a value and you don't assign it to something, it gets put in the variable "_" . Perhaps my example should have read:

>>> line = myfile.readline()
>>> line.rstrip()
'John Doe'

You can find the details about .rstrip() and other string methods here: http://docs.python.org/lib/string-methods.html

To read the entire contents of a file, use .read() rather than .readline()

alvin murphy

Posts: 4
Nickname: amurphy
Registered: Oct, 2004

Re: writing string variable to file Posted: Oct 21, 2004 11:57 PM
Reply to this message Reply
Thanks for your answer. I am puzzled. My code works if I type it in line by line in the IDLE command line or the IDLE GUI or if I paste it in line by line from the IDLE editor. It does not run (successfully if I run it from the editor by selecting "run module" or from the gui or command line by using "import". I have tried it on three machines, one with Win XP, one with Win 2000, one with Win 98; same result. I have not yet tried it on my linux machine which is not working. I will try to paste the file in below. Thanks also for the other comments.

str_Name = raw_input("Please enter your name")
myfile = open("myfile", "w")
myfile.write(str_Name)
myfile.close()
str_PresentIllness = raw_input("Please describe your problem")
myfile = open("myfile", "a")
myfile.write(str_PresentIllness)
myfile.close()
myfile = open("myfile")
myfile.read()
myfile.close()
raw_input()

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: writing string variable to file Posted: Oct 22, 2004 5:22 AM
Reply to this message Reply
I see... IDLE prints out the return value of a function if you don't assign it to something;

>>> myFunction()
'Blah'
>>> a = myFunction()
>>>

It's a feature for easy debugging I guess. But come normal execution, the return values are ignored, you have to explicitly display them.

Also:

- You can keep the file open and write both variables one after the other without closing the file each time.

- Think about seperating the two lines in the file with something.

alvin murphy

Posts: 4
Nickname: amurphy
Registered: Oct, 2004

Re: writing string variable to file Posted: Oct 22, 2004 5:07 PM
Reply to this message Reply
Thanks for the tip about not having to close the file twice or each time I write to it. These are certainly baby steps, and I would never imagine a program that would write to a file and immediately read from it; I imagine the "reading" would occur much later, perhaps via word processor, with formatting etc.. Anyway I still do not understand your answer. Pardon if I use the wrong terminology, but I know my "write" statements are working because I can manually open myfile after the the little program has run and find the text; what I do not understand is why the "read" statement or command does not produce any text on the GUI screen when the whole thing is run as a module from the editor. in other words, it runs all the commands or statements including the raw_input, open, close, write, but not read. Thanks again.

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: writing string variable to file Posted: Oct 23, 2004 5:27 AM
Reply to this message Reply
I meant;

print myfile.readline()

rather than just;

myfile.readline()

Selena Blane

Posts: 2
Nickname: sharlotta
Registered: Feb, 2019

Re: writing string variable to file Posted: Feb 6, 2019 2:44 AM
Reply to this message Reply
String concatenation:

plot.savefig ('hanning' + str (num) + '.pdf')

Conversion qualifier:

plot.savefig ('hanning% s.pdf'% num)

Using local variable names:

plot.savefig ('hanning% (num) s.pdf'% locals ()) # Neat trick

Using format ():

plot.savefig ('hanning {0} .pdf'.format (num)) # Note: This is the new preferred way

Using f-lines:

plot.savefig (f'hanning {num} .pdf ') # added in Python 3.6

Using string.Template:

plot.savefig (string.Template ('hanning $ {num} .pdf'). substitute (locals ()))

Also look https://pro-papers.com

Gordon Blue

Posts: 1
Nickname: solange
Registered: Apr, 2019

Re: writing string variable to file Posted: Apr 1, 2019 3:29 PM
Reply to this message Reply
Wow, cool code, we have services in the company that help us complete assignments and good writers
on subjects with programming.

Flat View: This topic has 9 replies on 1 page
Topic: python question Previous Topic   Next Topic Topic: Can you write a factorial function?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use