多亏了stackoverflow,我能够读取和复制文件。但是,我需要一次一行地读取图片文件,并且缓冲区数组不能超过3,000个整数。我该如何分开这些行,阅读它们,然后复制它们?这是最好的执行方式吗?
这是我的代码,由@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()会返回一个字符串,而不是整数。另外,我如何限制它处理的整数的数量?
我想你应该这样做:
infile = open(source,'r')
file_contents_lines = infile.readlines()
for line in file_contents_lines:
print line
这将获得文件中的所有行,并将它们放入一个列表中,其中每行作为列表中的一个元素。
请看这里的文档