[Tutor] Help with file I/O.

Nathan Pinno falcon3166 at hotmail.com
Mon Aug 1 00:36:36 CEST 2005


I fixed it. Here is the latest code:
filename = raw_input("File name please: ")
f = file(filename, "r")
for line in f.readlines():
    print line
f.close()

Thanks for taking the time to explain the writing part, that was really 
baffling me. Now I'm first going to write the main part of a password 
program, then when that's solid, I'll add the file I/O. Maybe I'll try the 
exercise where I have to add file I/O to a grades program, in the 
Non-Programmer's Tutorial for Python.
----- Original Message ----- 
From: "luke" <rabidpoobear at gmail.com>
To: "Nathan Pinno" <falcon3166 at hotmail.com>; "Danny Yoo" 
<dyoo at hkn.eecs.berkeley.edu>
Cc: <tutor at python.org>
Sent: Sunday, July 31, 2005 4:04 PM
Subject: Re: [Tutor] Help with file I/O.


> Nathan,
>
> I saw in your previous example that you called
> #quote
> file = raw_input("File name please: ")
> f = file(file, "r")
> for line in f.readlines():
>    print line
> f.close()
> #endquote
>
> the reason you were getting an error is that
> you made a variable named "file" on line one,
> which then overwrote the builtin method
> "file" in your namespace.
> therefore, the compiler throws a
> "string not callable error"
> because it's trying to call "File name please: "(file, "r")
> which of course doesn't work.
>
> What you want to do is make __sure__
> that you never name a variable the same thing as a function
> unless you're sure that's what you want to do.
> I believe some poeple recommend that you use
> nouns for variables (because they're things)
> and verbs for functions (because it's an action)
> but in this case,
> just make sure not to use "file"
> or "open" or "str" or "int" or anything as variable names.
>
> as for your other question,
>
>> Okay I understand how to open and read to a file, but how do I write to a
>> file, e.g. a list.
>
> you should really read the tutorial and try to figure it out before asking
> us.
> I am going to give you the answer but don't keep reading if you want to
> figure it out yourself.
>
> def WriteToFile(listoflines,filename="default.txt"):
>  f = file(filename, "w")
>  f.writelines(listoflines)
>  f.close()
>
> def main(args):
>  text = ["hello\r\n","Good Morning nathan.\r\n"]
>  filename = ""
>  for arg in args:
>    if arg == "-f" or arg == "--filename":
>      grab_next_arg = 1
>      continue
>    if grab_next_arg:
>      filename = arg
>      break
>
>  if filename != "":
>    WriteToFile(text,filename)
>  else:
>    WriteToFile(text)
>
> hope that helps.
> -Luke
>
>> ----- Original Message -----
>> From: "Nathan Pinno" <falcon3166 at hotmail.com>
>> To: "Nathan Pinno" <falcon3166 at hotmail.com>; "Danny Yoo"
>> <dyoo at hkn.eecs.berkeley.edu>
>> Cc: <tutor at python.org>
>> Sent: Sunday, July 31, 2005 2:46 PM
>> Subject: Re: [Tutor] Help with file I/O.
>>
>>
>> > Here's the improved version.
>> > file = raw_input("File name please: ")
>> > f = open(file)
>> > for line in f.readlines():
>> >    print line
>> > f.close()
>> >
>> > ----- Original Message -----
>> > From: "Nathan Pinno" <falcon3166 at hotmail.com>
>> > To: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
>> > Cc: <tutor at python.org>
>> > Sent: Sunday, July 31, 2005 2:29 PM
>> > Subject: Re: [Tutor] Help with file I/O.
>> >
>> >
>> >> Here's my work. I call it filewriter.
>> >> The code:
>> >> file = raw_input("File name please: ")
>> >> f = file(file, "r")
>> >> for line in f.readlines():
>> >>    print line
>> >> f.close()
>> >>
>> >> Will it do the trick?
>> >>
>> >> Nathan
>> >> ----- Original Message -----
>> >> From: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
>> >> To: "Nathan Pinno" <falcon3166 at hotmail.com>
>> >> Cc: <tutor at python.org>
>> >> Sent: Sunday, July 31, 2005 12:22 AM
>> >> Subject: Re: [Tutor] Help with file I/O.
>> >>
>> >>
>> >>>
>> >>>
>> >>> On Sun, 31 Jul 2005, Nathan Pinno wrote:
>> >>>
>> >>>> Well, you saw my password program. That was my first attempt at 
>> >>>> using
>> >>>> file I/O. Thought I'd try it big time. You saw where that went.
>> >>>
>> >>> Ok, let's take a look.  It was from this message, right?
>> >>>
>> >>>    http://mail.python.org/pipermail/tutor/2005-July/039478.html
>> >>>
>> >>> That was such a while back that I think you probably learned a lot
> since
>> >>> then, and I think a few of the issues there were less about I/O and
> more
>> >>> about just general programming.
>> >>>
>> >>> Let's try a few things, just to see why you're getting stuck.  Can 
>> >>> you
>> >>> write a program that reads in a file, and just prints all of its 
>> >>> lines
>> >>> out
>> >>> to screen?
>> >>>
>> >>>
>> >> _______________________________________________
>> >> Tutor maillist  -  Tutor at python.org
>> >> http://mail.python.org/mailman/listinfo/tutor
>> >>
>> >
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
> 


More information about the Tutor mailing list