开发者

Read/Write files problem

I`ve been trying to copy a long text from one file to another file b开发者_JAVA百科ut it always copied me just a small part of it. It looks like a limit problem which i can solve.

I would be glad if anyone can help me or explain which is my error.

def runMenu():

print "\nMENU"
print "  1) Copiar"
print "  0) Exit"

response = int( raw_input().strip() )

if response == 1:
    print "Copiar"       
    try:

        archivo=open("D:\Boot.txt","r")
        print "Name of the file: ", archivo.name
        print "Closed or not : ", archivo.closed
        print "Opening mode : ", archivo.mode

        print "--------ORIGEN-----------"
        print archivo.read()
        print "-------------------------"
        archivo.seek(0, 0)


        archivo2=open("D:\Copia.txt","w+")
        print "Name of the file: ", archivo2.name
        print "Closed or not : ", archivo2.closed
        print "Opening mode : ", archivo2.mode



        archivo2.write(archivo.read())

        archivo2.seek(0, 0)
        print "---------DESTINO---------"
        print archivo2.read()
        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()


Use shutil.copy()

http://docs.python.org/library/shutil.html#shutil.copy

shutil.copy(src, dst)

Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.

-Sunjay03


Is your file by any chance binary and not text? It could explain why only a part of the file is being copied. In any case, I suggest using the method already mentioned for copying.


Windows (at least up to and including XP, not sure about Vista and 7) can do strange things when you use fseek() or ftell() on a file opened in text mode. Since the Python functions seek() and tell() are only relatively thin wrappers around these functions, this applies to them as well.

I find that the best thing to do when doing file I/O in Windows is to simply open all files in binary mode. So, instead of open('somefile.txt', 'r'), you would do open('somefile.txt', 'rb') and open('some_other_file.txt','w+b').

Opening files in binary mode has no real downsides, but solves weirdness like this.


These four lines are interesting...

  1. archivo2.write(archivo.read())
  2. archivo2.seek(0, 0)
  3. print archivo2.read()
  4. archivo2.close()

On line 1, you pump some data out to the file; along the way, the operating system helpfully does some buffering to make the process more efficient. At the end of that, some of the data has been written to disk, but some of it is still lingering in the buffer waiting for the buffer to fill up.

On line 2, you reposition the file at the front of the file and on line 3, you read until the end of file. You never actually did anything that would cause the buffer to be written to disk.

On line 4, you close the file, but the buffer was invalidated.

You can tell python that you would like all the contents in a file's buffer written to disk, with file.flush(). What happens when you add archivo2.flush() between lines 1 and 2?


Maybe you have to call the read/write function (archivo2.write(archivo.read()) ) more often than once. Try to get it in an Loop and copy the file data until the "Eof" (end of file). Use a loop, something like this:

while not Eof(archivo) do
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜