2 回答

TA貢獻1820條經驗 獲得超3個贊
第一個參數open
是文件的路徑;如果只提供名稱,則使用當前目錄。
因此,您只需將所需目錄的路徑(有 os.path 函數可以幫助解決此問題)添加到文件名中。

TA貢獻1784條經驗 獲得超2個贊
首先你需要檢查目錄是否存在,如果不存在讓我們創建一個
而且您還需要更改文件的保存位置
這是一個例子
import os
def requesthandle( link, name ):
global THREAD_COUNTER
THREAD_COUNTER += 1
if not os.path.exists('/downloads'): #check if the folder is exist
os.makedir('/downloads') # if not let create one xD
try:
r = requests.get( link, stream=True )
if r.status_code == 200:
r.raw.decode_content = True
f = open( "/downloads/"+str(name) , "wb" ) # And here change where the file will be saved xD
shutil.copyfileobj(r.raw, f)
f.close()
print ("[*] Downloaded Image: %s" % name)
except Exception as error:
print ("[~] Error Occured with %s : %s" % (name, error))
THREAD_COUNTER -= 1
添加回答
舉報