[Tutor] Create file and input text

wesley chun wescpy at gmail.com
Sun Jun 29 04:56:35 CEST 2008


> Hi, I am very new to python and [...] 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?



david,

 welcome to Python!  ummmm, yes, it is ok.  your syntax is perfect, and
 it should've done exactly what you wanted.  and yes, Python is *that*
 intuitive and that easy. you're gonna *love* programming with it.  :-)

 one suggestion i *do* have is that your code only writes a single line
 of text to the file. if you intend on creating a text file with
 multiple lines, be sure to add a NEWLINE (\n) at the end of each
 string, otherwise you'll be writing everything all to one single,
 really long line.

 another idea is that you may wish to write some code that does the
 opposite: open a text file for read, display its contents, close the
 file, just as a test to ensure that your original app works as
 advertised.

 cheers!
 -- wesley
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 "Core Python Programming", Prentice Hall, (c)2007,2001
    http://corepython.com

 wesley.j.chun :: wescpy-at-gmail.com
 python training and technical consulting
 cyberweb.consulting : silicon valley, ca
 http://cyberwebconsulting.com


More information about the Tutor mailing list