9 Replies - 11174 Views - Last Post: 18 July 2011 - 05:11 AM Rate Topic: -----

#1 bolla3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 11

taking user input and writing to text file

Posted 16 July 2011 - 06:41 AM

I am trying to make my program open a .txt file, taking user input and the writing it to the text file.
I understand that I need to use input(). But I don't understand how to prompt the user for input and how to make python write to the text file. So far I have used
f = open('document.txt', 'w')

And trying to use
f.write('text')

To write. But I can't find a way to get it to write the input instead of text provided in the code.
Is there a way to do this?
Or at least another way to do it?

Is This A Good Question/Topic? 0
  • +

Replies To: taking user input and writing to text file

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: taking user input and writing to text file

Posted 16 July 2011 - 06:45 AM

This is actually fairly simple. What you need to do is use raw_input to ask the user for their import, store the input in a variable, and then pass that variable as the parameter for f.write().

Example:

foo = "bar"

f = open('document.txt', 'w')
f.write(foo)


Also, make sure to close the file when you're done by calling f.close().
Was This Post Helpful? 1
  • +
  • -

#3 bolla3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 11

Re: taking user input and writing to text file

Posted 16 July 2011 - 06:57 AM

It works! Thank you :D

is there any way to make it easier to read? When I write two or more variables to the file they end up on the same line. Not even a space :( And in this case the user will need to provide input at least 5-6 times, sometimes more. Is there a way to format the text?
Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: taking user input and writing to text file

Posted 16 July 2011 - 12:08 PM

You need to insert the lineseparators yourself when writing to a file.
#asuming python3.x
#for py 2.x input should be raw_input
input1 = input("first input:")
input2 = input("second input:")
file = open("somefile.txt", "w")
file.write(input1 + "\n") #the \n is the line separator (on unix, might be different in windows/mac os)
file.write(input2 + "\n")
file.close()



or you might choose your own serializing scheme:
input1 = input("first input:")
input2 = input("second input:")
file = open("somefile.txt", "w")
file.write(input1 + "...myownseparator..." + input2 + "\n") #still the \n for separating lines in the file
file.close()


Was This Post Helpful? 1
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: taking user input and writing to text file

Posted 16 July 2011 - 01:48 PM

If you want to get really tricky with it, try this ;)

#asuming py 2.x 
input1 = raw_input("first input:")
input2 = raw_input("second input:")
file = open("somefile.txt", "w")
file.write("\n".join((input1,input2,)))
file.close()



Here's what this does "\n".join((input1,input2,)):

we put the strings into a tuple,
then we create a string (stringA) '\n' and then call on that string's join method and pass the tuple as an argument,
Join essentially takes every element from the iterable object you passed in and joins them with stringA between each element.

This post has been edited by atraub: 16 July 2011 - 02:09 PM
Reason for edit:: parameters are not the same thing as arguments... sigh, I gotta stop using them interchangably

Was This Post Helpful? 1
  • +
  • -

#6 bolla3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-July 11

Re: taking user input and writing to text file

Posted 16 July 2011 - 01:57 PM

Hallelujah! Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#7 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: taking user input and writing to text file

Posted 16 July 2011 - 06:23 PM

I personally like string formatting.

input1 = raw_input("Input: ")
input2 = raw_input("Input: ")
file = open("document.txt", "w")
file.write("InputOne: %s \n InputTwo: %s" % input1, input2)

Was This Post Helpful? 0
  • +
  • -

#8 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: taking user input and writing to text file

Posted 16 July 2011 - 08:29 PM

I don't tend to recommend that because it's deprecated as of python 3.1.

This post has been edited by atraub: 16 July 2011 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 sean.orear  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 12
  • Joined: 21-December 10

Re: taking user input and writing to text file

Posted 17 July 2011 - 07:07 AM

file.write("Input One: {0} \n Input Two: {1}".format(input1, input2)) will work in both python2.6/2.7 and python3. However, I would use the .join() method shown above as well.
Was This Post Helpful? 0
  • +
  • -

#10 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: taking user input and writing to text file

Posted 18 July 2011 - 05:11 AM

View Postatraub, on 17 July 2011 - 12:59 AM, said:

I don't tend to recommend that because it's deprecated as of python 3.1.


Is it really? That kind of makes me want to cry a little. :<
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1