python - 读/写文件问题

标签 python file

我一直试图将一个长文本从一个文件复制到另一个文件,但它总是只复制了一小部分。它看起来像一个我可以解决的极限问题。

如果有人能帮助我或解释我的错误,我会很高兴。

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()

最佳答案

使用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

关于python - 读/写文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7166220/

相关文章:

python - 将记录收集到 Eve/mongodb 中的单个数组中以减少带宽

python - 如何为时间戳(日期时间)的数组指定 psycopg2 参数

python - 字符串的常量部分

Python - 处理传递到重定向处理程序的损坏的 URL?

java - LibGdx 无法从目录中获取文件

python - 打开外部代码时Rpy2解析错误

node.js - 使用 Node.JS(LTS) 读取大文件 (>~10Gb) 的最有效方法是什么?

Python:使用 csv 模块有效读取文件

c - 如何从文件中读取最后 2 个字节并在读取后删除它们

linux - 如何提取 filename.tar.gz 文件