如何在python中读取和分割文件的各行?

如何在python中读取和分割文件的各行?,python,file,buffer,Python,File,Buffer,由于stackoverflow,我能够读取和复制文件。但是,我需要一次读取一行图片文件,缓冲区数组不能超过3000个整数。我该如何把这些行分开,读出来,然后复制出来?这是执行此操作的最佳方式吗 这是我的代码,由@Chayim提供: import os import sys import shutil import readline source = raw_input("Enter source file path: ") dest = raw_input("Enter destination

由于stackoverflow,我能够读取和复制文件。但是,我需要一次读取一行图片文件,缓冲区数组不能超过3000个整数。我该如何把这些行分开,读出来,然后复制出来?这是执行此操作的最佳方式吗

这是我的代码,由@Chayim提供:

import os
import sys
import shutil
import readline

source = raw_input("Enter source file path: ")
dest = raw_input("Enter destination path: ")

file1 = open(source,'r')


if not os.path.isfile(source):
    print "Source file %s does not exist." % source
    sys.exit(3)
    file_line = infile.readline()

try:
    shutil.copy(source, dest)

    infile = open(source,'r')
    outfile = open(dest,'r')

    file_contents = infile.read()
    file_contents2 = outfile.read()

    print(file_contents)
    print(file_contents2)

    infile.close()
    outfile.close()

except IOError, e:
    print "Could not copy file %s to destination %s" % (source, dest)
    print e
    sys.exit(3)
我补充说 file_line=infle.readline()
但我担心infle.readline()将返回字符串,而不是整数。另外,我如何限制它处理的整数数量?

我想您应该这样做:

infile = open(source,'r')

file_contents_lines = infile.readlines()

for line in file_contents_lines:
    print line
这将获取文件中的所有行,并将它们放入一个列表中,该列表包含作为列表中元素的每一行


看看这里的问题。

您的问题具体是什么?您希望上述程序做什么?谢谢!我一定要看一下医生。@BabyBlueLion如果这回答了你的问题,你能把这标记为接受的答案吗?谢谢