1

I would like to know, how I could make my code add each input on a new line in a file, but not add an extra empty line. Currently, I am using "\n" to write append each input on a new line, but this adds an extra empty line. Is there any way to do this without adding the extra line?

Here is all of my code:-

f= open("Passes.py", "a+")
m=open("money.py","r+")
passes= {}
init={}
initial=0
import time
print "Welcome to the virtual banking system"
user=raw_input("Would you like to create a new account? 'Y/N'").lower()
if user== "y":
  new_user= raw_input("Create a username:")
  new_pass= raw_input("Create a password:")
  p= passes[new_user]= new_user + ":" + new_pass
  f.write("\n"+p)
  ask=raw_input("Would you like to sign into your account? 'Y/N'").lower()
  if ask=="y":
    user_in=raw_input("Enter your username:")
    if user_in==new_user:
      pass_in=raw_input("Enter your password:")
      if pass_in==new_pass:
        print "Welcome to your account" + " " + new_user
        useropt=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt=="1":
          print "Your balance is:", initial
        if useropt=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          initial+=amountdep
          print "Thanks. Your new balance is:", initial
        if useropt=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          initial-=amountwith
          print "Your balance is:", initial
        i=init[new_user]=str(initial)
        m.write(str(i)+"\n")
      else:
        print "Password not valid"

    else:
      print "Username does not exist"

  else:
    print "Thanks for using the virtual bank."

else:
  user2=raw_input("Do you have an existing account? 'Y/N'").lower()
  if user2=="y":
    existing_user=raw_input("Enter your username:")
    exisitng_pass=raw_input("Enter your password:")
    for passwords in f:
      if passwords==existing_user+":"+exisitng_pass:
        print "Welcome to your account" + " " + existing_user
        useropt2=raw_input("Would you like to view your balance- enter 1, deposit money- enter 2, withdraw money- enter 3 or exit- enter 4:")
        if useropt2=="1":
          for info in m:
            print "Your balance is:", info
        if useropt2=="2":
          amountdep= int(raw_input("How much money would you like to deposit?:"))
          for info in m:
            a=int(info)+int(amountdep)
            print "Thanks. Your balance is:", int(a)
          m.close()
          m= open("money.py", "w")
          m.write(str(a))
        if useropt2=="3":
          amountwith=int(raw_input("How much would you like to withdraw?:"))
          for info in m:
            t=int(info)-int(amountwith)
            print "Thanks. Your balance is:", t
          m.close()
          m=open("money.py","w")
          m.write(str(t))
7
  • 1
    If you use print, it automatically adds a new line character, so do not include "\n" in what you print. Post some example code if this doesn't solve your question. Commented Jan 27, 2017 at 0:06
  • I did not use print, but I just write to the file Commented Jan 27, 2017 at 0:07
  • f.write(p+"\n") Commented Jan 27, 2017 at 0:07
  • Can you please post the code that you're using to write, and the code for the input? Commented Jan 27, 2017 at 0:09
  • 3
    maybe your data already have '\n' at the end and you forgot to strip() it. BTW: when you read lines from file then it doesn't remove '\n' so you have to remove it manually. Commented Jan 27, 2017 at 0:16

2 Answers 2

1

What you could do to prevent the trailing newline is to print the newline at the beginning of your string. This would obviously introduce a blank line at the beginning of the file. To prevent that you could use tell() to check if the string about to be printed is going to be the first line in the file and if so skip the newline. That should remove the issue of trailing newlines. This would look something like this:

position = m.tell()
if position == 0:
    m.write(str(i))
else:
    m.write('\n'+str(i))
Sign up to request clarification or add additional context in comments.

Comments

0

I believe all you need to do is instead of:

m.write(str(i)+"\n")

You want to do:

m.write(str(i))

Also, you have a minor typo; when you use the existing_pass variable you spell it exisitng_pass, but your code runs because it is misspelled both times it is used.

1 Comment

The problem with this though, is I still want the file to write on new lines, I just don't want there to be an empty line at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.