以下のスニペットを使用してファイルを読み取り、データを使用してクラウドからダウンロードしています。このコードは、Windows と Mac でも完全に問題ありません。ただし、Linux インスタンスにログインして ssh でスクリプトを実行すると、スクリプトが失敗します。
後で、以下のコマンドを使用してファイルを実行しましたが、ある時点までは機能しているようで、後で失敗しました。
コマンド: PYTHONIOENCODING=utf-8 python3 ファイル名.py テキストファイル名
def file_block(fp, number_of_blocks, block):
'''
A generator that splits a file into blocks and iterates
over the lines of one of the blocks.
'''
assert 0 <= block and block < number_of_blocks
assert 0 < number_of_blocks
fp.seek(0,2)
file_size = fp.tell()
ini = file_size * block / number_of_blocks
end = file_size * (1 + block) / number_of_blocks
if ini <= 0:
fp.seek(0)
else:
fp.seek(ini-1)
fp.readline()
while fp.tell() < end:
yield fp.readline()
def download_files(conn,container_name,number_of_chunks, chunk_number, file_name):
fp = open(file_name, encoding='utf-8')
counter = 0
try:
for line in file_block(fp,number_of_chunks, chunk_number):
counter = counter + 1
clean_object_name = str(bytes(line, encoding='utf-8').decode('utf-8', 'ignore')).rstrip('\n\r ')
try:
if not os.path.exists(os.path.dirname(clean_object_name)):
os.makedirs(os.path.dirname(clean_object_name))
if os.path.basename(clean_object_name) != '':
obj_tuple = conn.get_object(container_name, clean_object_name)
with open(clean_object_name, 'wb') as f:
f.write(obj_tuple[1])
print("Successfull ", current_process().name, " ", counter , " " ,clean_object_name.encode('utf-8'), "\n")
except:
sys.exc_info()[2]
if not os.path.exists("log"):
os.mkdir("log")
with open("log/" + "log_" + current_process().name + ".txt", 'a', encoding='utf-8') as f:
try:
print("Failed counter ", counter, " " ,clean_object_name.encode('utf-8'))
f.write("missing " + clean_object_name.encode('utf-8') + "\n")
f.write("traceback " + sys.exc_info()[2] + "\n")
except:
f.write("missing " + str(counter) + "\n")
except:
sys.exc_info()[2]
if not os.path.exists("process_failure_log"):
os.mkdir("process_failure_log")
with open("process_failure_log/" + "log_" + current_process().name + ".txt", 'a', encoding='utf-8') as f:
try:
f.write("process failed while reading the file at counter " + str(counter) + "\n")
f.write(str(sys.exc_info()[2]) + "\n")
except:
f.write("missing " + str(counter) + "\n")
データの下に含まれるテキスト ファイル:
user_photos/images/282/onehundred/Capture d’écran 2012-09-07 à 2.50.31 PM20120917-37935-13g7sn1-0_1347875141.png
user_photos/images/282/original/Capture d’écran 2012-09-07 à 2.50.31 PM20120917-37935-13g7sn1-0_1347875141.png
user_photos/images/282/preview/Capture d’écran 2012-09-07 à 2.50.31 PM20120917-37935-13g7sn1-0_1347875141.png
user_photos/images/282/thumbnail/Capture d’écran 2012-09-07 à 2.50.31 PM20120917-37935-13g7sn1-0_1347875141.png
user_photos/images/282/twohundred/Capture d’écran 2012-09-07 à 2.50.31 PM20120917-37935-13g7sn1-0_1347875141.png
user_photos/images/283/onehundred/Capture d’écran 2012-09-11 à 6.21.50 PM20120917-38000-37awsu-0_1347875181.jpg
user_photos/images/283/original/Capture d’écran 2012-09-11 à 6.21.50 PM20120917-38000-37awsu-0_1347875181.jpg
user_photos/images/283/preview/Capture d’écran 2012-09-11 à 6.21.50 PM20120917-38000-37awsu-0_1347875181.jpg
user_photos/images/283/thumbnail/Capture d’écran 2012-09-11 à 6.21.50 PM20120917-38000-37awsu-0_1347875181.jpg
user_photos/images/283/twohundred/Capture d’écran 2012-09-11 à 6.21.50 PM20120917-38000-37awsu-0_1347875181.jpg
user_photos/images/284/onehundred/Capture d’écran 2012-09-11 à 6.20.56 PM20120917-38101-6po8vq-0_1347875238.jpg
user_photos/images/284/original/Capture d’écran 2012-09-11 à 6.20.56 PM20120917-38101-6po8vq-0_1347875238.jpg
user_photos/images/284/preview/Capture d’écran 2012-09-11 à 6.20.56 PM20120917-38101-6po8vq-0_1347875238.jpg
user_photos/images/284/thumbnail/Capture d’écran 2012-09-11 à 6.20.56 PM20120917-38101-6po8vq-0_1347875238.jpg
user_photos/images/284/twohundred/Capture d’écran 2012-09-11 à 6.20.56 PM20120917-38101-6po8vq-0_1347875238.jpg
上記のコマンドを使用した後、ファイルの読み取りと書き込みはできましたが、スクリプトは以下の点で失敗しました:
with open(clean_object_name, 'wb') as f:
f.write(obj_tuple[1])
トレースバック:
'ascii' codec can't encode character in position 55-56: ordinal not in
range(128).
エキセントリックな性格のせいだとわかっています。デコードメソッドを使用できます。しかし、ファイル名を認識できない文字に置き換えたくありません。
混乱しました。エンコーディングが問題のスクリプトである場合、初期段階で失敗するはずです。しかし、スクリプトは他のファイルを読み書きするときに正常に動作します。風変わりなキャラクターでファイルを作成した場合のみ失敗します。提案してください、私はこれにぶら下がっているだけで丸2日の仕事を無駄にしました. コードは、windows と mac で完全に動作します。