0

感谢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 = infile.readline() 但我担心 infile.readline() 将返回一个字符串,而不是整数。另外,我如何限制它处理的整数数量?

4

1 回答 1

2

I think you want to do something like this:

infile = open(source,'r')

file_contents_lines = infile.readlines()

for line in file_contents_lines:
    print line

This will get you all the lines in the file and put them into a list containing each line as an element in the list.

Take a look at the documentation here.

于 2013-03-06T15:52:31.230 回答