def花 发表于 2020-8-16 17:50:11

保存不了指定的文件夹内

import urllib.request
import easygui as g

title = '下载一只猫'
msg = '请输入猫的尺寸'
filelds =['宽:','高:']
input1 = g.multenterbox(msg,title,filelds)
url = 'http://placekitten.com/%s/%s' % (input1,input1)
response = urllib.request.urlopen(url)
cat_image = response.read()
with open('cat.jpg','wb') as f:
    f.write(cat_image)
g.filesavebox(default = 'cat.jpg',filetypes = ['.jpg'])

就是保存不了指定的文件夹内

Twilight6 发表于 2020-8-16 18:09:12

本帖最后由 Twilight6 于 2020-8-16 18:10 编辑



改成这样,你直接 with而且 open 填 cat.jpg 只是写入当前文件夹下了

import urllib.request
import easygui as g

title = '下载一只猫'
msg = '请输入猫的尺寸'
filelds =['宽:','高:']
input1 = g.multenterbox(msg,title,filelds)
url = 'http://placekitten.com/%s/%s' % (input1,input1)
response = urllib.request.urlopen(url)
cat_image = response.read()
path = g.filesavebox(default = 'cat.jpg',filetypes = ['.jpg'])

with open(path,'wb') as f:
    f.write(cat_image)

hrp 发表于 2020-8-16 18:11:26

试试这样import urllib.request
import easygui as g

title = '下载一只猫'
msg = '请输入猫的尺寸'
filelds =['宽:','高:']
input1 = g.multenterbox(msg,title,filelds)
url = 'http://placekitten.com/%s/%s' % (input1,input1)
response = urllib.request.urlopen(url)
cat_image = response.read()
savepath = g.filesavebox(default = 'cat.jpg',filetypes = ['.jpg'])
with open(savepath,'wb') as f:
    f.write(cat_image)
页: [1]
查看完整版本: 保存不了指定的文件夹内