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
添加回答
举报