473,699 Members | 2,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to add user input to file and print

3 New Member
Here are my code below, i have two issues: 1) user input was overwritten. 2)i don't see the line is printed. What did i miss? please help.
------------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. #!usr/local/bin/python3
  2. f = open('python1/inputter.txt', 'w')
  3. while True:
  4.     i = input("Write a sentence:")
  5.     f.write(i)
  6.     if i == "":
  7.         break
  8. f = open('python1/inputter.txt', 'r')
  9. f.read()
  10. for line in f:
  11.     print(line)
Sep 23 '12 #1
4 2497
zmbd
5,501 Recognized Expert Moderator Expert
line 2, file opened for write not append.
http://docs.python.org/library/functions.html.
Also between 7 and 8 you do not explicitly close the file... this is highly recommended to do (section 7.2.1 just before 7.2.2): http://docs.python.org/tutorial/inputoutput.html
Sep 23 '12 #2
newbieinpython
3 New Member
@zmbd
Thanks for reponse. But I still don't see you are helping with my problem.
Sep 23 '12 #3
zmbd
5,501 Recognized Expert Moderator Expert
read the documentation I linked for you.
Sep 23 '12 #4
dwblas
626 Recognized Expert Contributor
Note that
f.write(i)
sends the data to the buffer to be written to file at some future time as it is more efficient to read or write a group of records at once. Also, the file will have zero length (no data) when you read it because the end-of-file pointer is generally updated when the file is closed and you never close the file. So you have to explicitly close the file which will flush the buffers and update to the correct length. Also, to print the contents you should use
print f.read()
A link to an on-line book on file handling and the use of readlines() plus iterating over the file. [edited to remove off-topic comments]
Sep 23 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
8231
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a program that reads 10 numbers from the user and prints out the sum of those numbers. num0 = input("Enter a number: ")
2
2383
by: Kyle E | last post by:
Ok, I have a little tiny problem, a beginner problem, that I am overlooking. I am writing a Information Gathering Program, that takes user input from scripted questions and prints them in a handy list in the end. As of now, when one of my questions is asked, the only possible way to enter text is to type ( "John" ) minus the parenthesis. You do need the qoutation marks though. I just want to be able to type ( John ) without the...
3
3435
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What I want to do is allow the user to select one of these regular expressions to modify, but I've realized I know of no way to provide a default value for user input. I could choose to show the regular expression the user has chosen and simply allow...
6
2522
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop. This part of the program is running fine. Every thing in the CarDealerApp.java file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the...
0
865
by: Contadino | last post by:
Hi all, I'm new to VB, using VB.Net express edition. I'm collecting data from a data acquisition card and writing the data to a .txt file. I have an input box for the user to input a file name. If the file already exists I want to warn the user and ask if they want to over write the file. I'm using MsgBox style YesNo. If they say yes, no problem. But if they say no, what do I do? I did a bit of searching but couldn't find a recommended...
4
2132
by: myself2211 | last post by:
Hi, this is my first attempt at Python, what I am trying to achieve is a script that will ask the user for input, search a txt file and display the output or give not found message. Ideally I would like it to ask the user once the first output is achieved for further user input if required, otherwise exit the program. However, would be happy if it achieved the first objective. Following is as far as I can go, the problem is it is breaking...
4
2722
by: ennoil | last post by:
I am requesting user input from a menu a script. The input I am requesting needs to be an integer. Is it possible to validate that the user actually entered an integer and not a string or just hit enter? Everything I have tried will give me "File "<string>", line 0, in ? NameError: name 'd' is not defined The code I have looks like this: def init_menu(): print ''' 0) Exit 1) Environment 2) Use
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9196
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8941
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.