2013-03-06 65 views
0

感谢stackoverflow,我能够读取和复制文件。但是,我需要一次读取一行图片文件,并且缓冲区数组不能超过3,000个整数。我将如何分隔线条,阅读它们,然后复制它们?这是执行此操作的最佳方式吗?如何在python中读取和划分文件的各个行?

这里是我的代码,@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()将返回一个字符串,而不是整数。另外,我如何限制它处理的整数数量?

+2

你的问题具体是什么?你想要上面的程序做什么? – theAlse 2013-03-06 15:43:54

回答

2

我想你想要做这样的事情:

infile = open(source,'r') 

file_contents_lines = infile.readlines() 

for line in file_contents_lines: 
    print line 

这将让你在文件中的所有行,并把它们放到含有每行列表中的一个元素的列表。

请看这里的documentation

+0

谢谢!我一定会看看文档。 – 2013-03-06 17:47:34

+0

@BabyBlueLion如果这回答了您的问题,您是否可以将此标记为接受的答案?谢谢! – 2013-03-06 18:06:43

相关问题