File write() problem

A

apriebe47

Alright, I realize this is probably very basic to be posted on this
newsgroup but I cannot figure out what is causing my problem. Here is
the code I am using below:

from getpass import getpass

configfile = file('config.txt', 'w')
serverPassword = configfile.readline()
if serverPassword == '':
print "Server warning: No password has been set!"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
while pwd1 != pwd2:
print "Passwords did not match."
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
print "Password set. Storing to file"
configfile.write(pwd2)
serverPassword = configfile.readline()
configfile.close()

Now when I look at the file, I can see the password that I had typed
in, but there are also a bunch of symbols that are added to the text
file as well. My question is how can I write just the password to the
text file and not any extra 'garbage'. Thanks!

Andrew
 
M

Marc 'BlackJack' Rintsch

apriebe47 said:
configfile = file('config.txt', 'w')
serverPassword = configfile.readline()

Here you open a file for writing and try to read a line. This raises an
exception:

In [1]: configfile = file('test.txt', 'w')

In [2]: configfile.readline()
---------------------------------------------------------------------------
exceptions.IOError Traceback (most recent call last)

/home/marc/<ipython console>

IOError: [Errno 9] Bad file descriptor


Please post the exact code you have trouble with. Don't retype it -- copy
and paste it.

Ciao,
Marc 'BlackJack' Rintsch
 
A

apriebe47

Hrm. When I run the code that I posted I don't seem to get those
errors.
Please post the exact code you have trouble with. Don't retype it -- copy
and paste it.

from getpass import getpass

configfile = file('config.txt', 'w')
serverPassword = configfile.readline()
if serverPassword == '':
print "Server warning: No password has been set!"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
while pwd1 != pwd2:
print "Passwords did not match."
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
print "Password set. Storing to admin/config..."
configfile.write(pwd2)
serverPassword = configfile.readline()
configfile.close()

Again I tested the code before I posted and I get no errors, just not
the result I am looking for in the text file. Thanks for taking the
time to look at it.

Andrew
 
R

rzed

(e-mail address removed) wrote in

Hrm. When I run the code that I posted I don't seem to get those
errors.


from getpass import getpass

configfile = file('config.txt', 'w')
serverPassword = configfile.readline()
if serverPassword == '':
print "Server warning: No password has been set!"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
while pwd1 != pwd2:
print "Passwords did not match."
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
print "Password set. Storing to admin/config..."
configfile.write(pwd2)
serverPassword = configfile.readline()
configfile.close()

Again I tested the code before I posted and I get no errors,
just not the result I am looking for in the text file. Thanks
for taking the time to look at it.

As Marc points out, you are opening the file in write mode and
then reading from it. Did you try printing out what gets placed
into serverPassword the first time? It may not be what you expect,
for starters. But now the file pointer is at the end of the file,
and that's where you write the input password. Amazingly, Python
lets you do this, but even if you could read back those characters
you just wrote by doing a readline() (which might be getting data
from the ether by now), the password wouldn't be written over the
original, but instead would be on the next line.

If your intent is to update a flat file, you can try using seeks
and rewinds and such to position the file pointer appropriately,
but you'll still be in misery as soon as someone replaces the
password "G811ploo" with "ALongerPassword". Flat files aren't
databases. You're better off reading the entire file (opened in
read mode) into memory and closing it, then re-opening it in write
mode and writing out the entire thing with the changed password,
replacing the original file.
 
T

Tim Roberts

Alright, I realize this is probably very basic to be posted on this
newsgroup but I cannot figure out what is causing my problem. Here is
the code I am using below:

from getpass import getpass

configfile = file('config.txt', 'w')
serverPassword = configfile.readline()
if serverPassword == '':
print "Server warning: No password has been set!"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
while pwd1 != pwd2:
print "Passwords did not match."
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
print "Password set. Storing to file"
configfile.write(pwd2)
serverPassword = configfile.readline()
configfile.close()

Now when I look at the file, I can see the password that I had typed
in, but there are also a bunch of symbols that are added to the text
file as well. My question is how can I write just the password to the
text file and not any extra 'garbage'. Thanks!

You want every file access to start at the beginning of the file. So,
there is really no point in maintaining a persistent file object at all:

FN = 'config.txt'
serverPassword = open(FN, 'r').readline()
if not serverPassword:
print "Server warning: No password has been set"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
while pwd1 != pwd2:
print "Passwords did not match"
pwd1 = getpass("New Password: ")
pwd2 = getpass("Confirm Password: ")
open(FN, 'w').write(pwd2+'\n')
serverPassword = open(FN, 'r').readline()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top