2016-11-28 10 views
0

私はjsonをループしてファイルに書き込み、curlコマンドを使用してアップロードするために別のfuntionに渡します。ここで iは取得していますエラーメッセージです:Python:ファイル操作でエラーが発生しました

Traceback (most recent call last): 
File "D:\python_ws\test.py", line 33, in <module> 
main((sys.argv[1:])) 
File "D:\python_ws\test.py", line 30, in main 
upload_file_bams(out_file) 
File "D:\python_ws\test.py", line 7, in upload_file 
file = open(rel_file).read() 
TypeError: coercing to Unicode: need string or buffer, file found 

私はさまざまな方法を試してみましたが、私はここにいくつかの基本をしないのですように思えます。どんな助けもありがとう。ここで

は私のコードです:

#!/usr/bin/env python 
import urllib2,json,sys,requests,subprocess,os 

def upload_file(rel_file): 
    url = "https://localhost:8080/artifactory/list/default.generic.local/ct/releases/" 
    user = "john.smith" 
    file = open(rel_file).read() 
    cmd = 'curl --verbose --user %s --upload-file %s %s' %(user,file,url) 
    print 'trying to execute %s' % cmd 
    x = subprocess.Popen('cmd', shell=True) 
    #subprocess.call('cmd', shell=True) 
    retval = x.wait() 

def main(argv): 
    #environment 
    env = sys.argv[1] 
    rel_name=sys.argv[2] 
    consul_url=("http://localhost:9090") % (env) 
    list_services = "/v1/catalog/services" 
    services_url = consul_url+list_services 
    list_of_services = urllib2.urlopen(services_url).read() 
    each_service = json.loads(list_of_services) 
    #to remove keys with blank values 
    newdict = dict([(vkey,vdata) for vkey, vdata in each_service.iteritems() if(vdata) ]) 
    try: 
     out_file = open(rel_name,"wb") 
     json.dump(newdict,out_file, indent=4) 
    finally: 
     out_file.close() 
    #uploading release json file to BAMS 
    upload_file(out_file) 

if __name__ == "__main__": 
     main((sys.argv[1:])) 
+2

Pythonコードを投稿する場合は、字下げを正しく再現してください。ひどくインデントされたPythonコードはナンセンスです。 – khelwood

+0

ありがとうございます。私はそれを確実にします。 – ryan1506

答えて

1

あなたがupload_file()を呼び出すと、あなたはタイプfileの代わりstring(ファイル名)であるout_fileそれを渡します。関数open()は、最初の引数のために開くファイル名をとります。

+0

ありがとうございます。私はそれを訂正した。私はCURLコマンドから応答を返さないようです。 – ryan1506

関連する問題