2017-09-05 1 views
0

私はpythonファイルと同じディレクトリに画像を持っています。画像をループしてbase64に変換しようとしていますが、このエラーが発生しています。 ディレクトリ内の画像をループする

Traceback (most recent call last): 
    File "convert_to_base64.py", line 33, in <module> 
    print(main()) 
    File "convert_to_base64.py", line 26, in main 
    convert_to_base64() 
    File "convert_to_base64.py", line 19, in convert_to_base64 
    with open("*.jpg", "rb") as f: 
IOError: [Errno 2] No such file or directory: '*.jpg' 

のUbuntu 14.0.4

使用していますがここでは私のpythonコードである

# -*- coding: utf-8 -*- 
import os 
import sys 
import xlrd 
import base64 
import urllib 
from datetime import datetime 

reload(sys) # to re-enable sys.setdefaultencoding() 
sys.setdefaultencoding('utf-8') 


def convert_to_base64(): 
    """ 
    Read all jpg images in a folder, 
    and print them in base64 
    """ 
    with open("*.jpg", "rb") as f: 
     data = base64.b64decode(f.read()) 
    print data 


def main(): 
    start_datetime = datetime.now() 
    convert_to_base64() 
    end_datetime = datetime.now() 
    print '------------------------------------------------------' 
    print 'Script started : {}'.format(start_datetime) 
    print 'Script finished: {}'.format(end_datetime) 

if __name__ == '__main__': 
    print(main()) 
    print('Done') 

誰かが私が間違ってやっているかを把握できます。 おかげ

答えて

1

これは私がディレクトリにある画像のためのループ方法です:

import os 

pictures = [] 
for file in os.listdir("pictures"): 
    if file[-3:].lower() in ["png"]: 
     pictures.append(file) 

は、open()関数の詳細はPythonドキュメントhttps://docs.python.org/2/tutorial/inputoutput.htmlを参照してください:

オープン()Aを返しますファイルオブジェクトであり、最も一般的にはopen(filename、mode)という2つの引数で使用されます。

関連する問題