By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

File write() problem

P: n/a
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

Dec 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In <11**********************@42g2000cwt.googlegroups. com>, apriebe47
wrote:
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
Dec 27 '06 #2

P: n/a
Marc 'BlackJack' Rintsch wrote:
configfile = file('config.txt', 'w')
serverPassword = configfile.readline()
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

Dec 27 '06 #3

P: n/a
ap*******@gmail.com wrote in
news:11**********************@h40g2000cwb.googlegr oups.com:
Marc 'BlackJack' Rintsch wrote:
configfile = file('config.txt', 'w')
serverPassword = configfile.readline()

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.
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.

--
rzed
.... for some value of better off...

Dec 27 '06 #4

P: n/a
ap*******@gmail.com wrote:
>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()
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.