0

stackoverflowのおかげで、ファイルを読み取ってコピーすることができます。ただし、画像ファイルを一度に1行ずつ読み取る必要があり、バッファ配列は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()が整数ではなく文字列を返すのではないかと心配しています。また、処理する整数の数を制限するにはどうすればよいですか?

4

1 に答える 1

2

私はあなたがこのようなことをしたいと思います:

infile = open(source,'r')

file_contents_lines = infile.readlines()

for line in file_contents_lines:
    print line

これにより、ファイル内のすべての行が取得され、リスト内の要素として各行を含むリストに入れられます。

こちらのドキュメントをご覧ください。

于 2013-03-06T15:52:31.230 に答える