reading and writing files

Dave Angel davea at
Wed Aug 24 06:05:40 CEST 2011

On 01/-10/-28163 02:59 PM, Adrián Monkas wrote:
> Hi.
> I`ve been trying to copy a long text from one file to another but it always
> copied me just a small part.
> I would be glad if you can help me or explain which is my error.
> Thanks
> ----------------------------------------------------------------------------------
> def runMenu():
>      print "\nMENU"
>      print "  1) Copiar"
>      print "  0) Exit"
>      response = int( raw_input().strip() )
>      if response == 1:
>          print "Copiar"
>          try:
>              print "Abro Archivo Origen"
>              archivo=open("D:\Boot.txt","r")
>              print "Name of the file: ",
>              print "Closed or not : ", archivo.closed
>              print "Opening mode : ", archivo.mode
>              print "--------ORIGEN-----------"
>              print
>              print "-------------------------"
>    , 0)
>              print "Abro Archivo Destino"
>              archivo2=open("D:\Copia.txt","w+")
>              print "Name of the file: ",
>              print "Closed or not : ", archivo2.closed
>              print "Opening mode : ", archivo2.mode
>              archivo2.write(
>    , 0)
>              print "---------DESTINO---------"
>              print
>              print "-------------------------"
>              archivo.close()
>              archivo2.close()
>          except IOError:
>              print ("I/O Error de Lectura")
>          else:
>              print "Lectura OK"
>      elif response == 0:
>          #device.close()
>          print "Exit"
>      return response
> def main():
>      print "main"
>      while(1):
>          if runMenu() == 0: break
> main()
What's your goal?  If it's to write a small program to copy a file, try 
using shutil library. It'll save you lots of trouble.  It can handle 
files that are bigger than available memory, it can fix up timestamps, etc.

If it's to learn Python, then you need to learn about writing debuggable 
code.  Part of that is doing things once.  So if you read() the entire 
contents of the file, keep it in a variable and use that variable to 
write() to the other file, and also to print to the screen.  Then if 
something's wrong, you can be sure it's the same both places.  Never do 
something like  y.write(  till you're sure everything's 
working.  That's a kind of optimization which doesn't save any execution 
time, and only a few keystrokes.  And you'll waste those keystrokes 
changing the code a dozen times to find out why it isn't working.

You don't make clear just what is "just a small part."  In other words, 
what is truncated?  Is it the ORIGEN printout, the DESTINO printout, or 
the actual file contents ?

And is the file involved a gigabyte or two, or is it 500 bytes?

Did it display part of the file correctly, or by "small part" did you 
mean zero bytes?

Show the actual stack trace if you got an error, or the program output 
if not.

And in general, when you're asking questions, please specify the Python 
version and OS version you're running.  Clearly, the latter is some kind 
of Windows, since you're using the D: drive.  And the Python version is 
2.x for some value of x.  But sometimes it'll matter.

Your filenames are incorrect, since you use the backslash without 
escaping it.  So the source file has a backspace in it.  I'm amazed you 
don't get an error, since it's unlikely you have a file with that kind 
of name.  When I run it, I get  "Abro Archivo Origen".  I'm astounded 
you don't.  When making a literal string out of a Windows filename, 
either double the backslashes, or use R"xxx" for raw strings, or just 
use forward slashes.



More information about the Python-list mailing list