Dave Kuhlman wrote:
On Sat, Jun 28, 2008 at 08:11:03PM -0400, David wrote:
Hi, I am very new to python and it is my first attempt at programing except for some basic bash scripts. I came up with this;
#!/usr/bin/python

import os
filename = raw_input('Enter the filename: ')
fobj = open(filename, 'w')
yourname = raw_input('What is your name: ')
fobj.write(yourname)
fobj.close()

It seems to work Ok, I was shocked! Is it OK?

It looks like good code to me.  But, one suggestion: It's dangerous
code, unless you can trust your users.  They can over-write files.  In
a real application, you might want to do some checking on the file
before opening it.  Consider using something like this:

    if os.path.exists(filename):
        print 'Warning.  File %s exists.' % filename
    else:
        fobj = open( ...


- Dave


Thanks Dave, cool name :)
here is what I came up with, seems to work as expected;
#!/usr/bin/python

import os
filename = raw_input('Enter the filename: ')
if os.path.exists(filename):
   print 'Warning.  File %s exists. -1 to quit' % filename
if (filename != -1):
   fobj = open(filename, 'w')
yourname = raw_input('What is your name: ')
fobj.write(yourname)
fobj.close()

--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to