Unjx. 2021-07-22 16:11 采纳率: 84.6%
浏览 20
已采纳

在Python中如何用open打开os创建的文件夹?

img


#encoding=utf-8
import requests
import re
import os

url='https://unsplash.com'
head={'User-Agent': 'Mozilla/5.0'}

rsp=requests.get(url,headers=head)
#print(rsp)
html=rsp.content.decode('utf-8')
# print(html)

pattern=re.compile(r'<img class=".*?srcSet="(.*?)"',flags=re.S)
image_list=pattern.findall(str(html))
print(image_list)

cwd=os.getcwd()
file_name = os.path.join(cwd,'图片')
if not os.path.exists('图片'):
    os.mkdir(file_name)

n=0
for src in image_list:
    image_data = requests.get(url=src, headers=head).content

    with open(file_name+str(n),'w')as f:
        f.write(str(image_data))
    n+=1

这是我的代码(这个代码还没写完!图片后缀我还不知道,要加上图片后缀才行)
问:像我上面说的一样,如何不使用绝对路径去打开那文件夹?
做事不要太绝嘛
如果我问题没描述清楚,还请指出

  • 写回答

1条回答 默认 最新

  • Feng-licong 2021-07-22 16:23
    关注
    with open(os.path.join(file_name,文件名),'w')as f
    

    可以这样子

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    问题事件

    • 已采纳回答 7月22日
    • 创建了问题 7月22日

    悬赏问题

    • ¥15 Tpad api账户 api口令
    • ¥30 ppt进度条制作,vba语言
    • ¥15 stc12c5a60s2单片机测光敏ADC
    • ¥15 生信simpleaffy包下载
    • ¥15 请教一下simulink中S函数相关问题
    • ¥15 在二层网络中,掩码存在包含关系即可通信
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能