V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zhuangzhelia9908
V2EX  ›  Python

云函数无法写入

  •  
  •   zhuangzhelia9908 · 2021-06-24 19:15:12 +08:00 · 1512 次点击
    这是一个创建于 1008 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import string
    import requests

    SCKEY = '666'
    url2 = 'https://api.tokyo.biliob233.com/user/check-in'
    cookie2 = '666'


    def it():
    headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36', 'cookie': cookie2
    }
    res = requests.post(url=url2, headers=headers).text
    f = open("bilibiliob.txt", 'w+')
    f.write(res)
    f.close()


    # 微信推送
    ssckey = SCKEY
    send_url = 'https://sc.ftqq.com/' + ssckey + '.send'
    with open('bilibiliob.txt', 'a+') as f2:
    text = f2.read()
    punctuation_string = string.punctuation
    for i in punctuation_string:
    text = text.replace(i, '')

    if 'code1msg 未登录' or 'code1msg 已经签过到了' in text:
    params = {
    'text': 'bilibiliob 签到失败提醒'
    }
    else:
    params = {
    'text': 'bilibiliob 签到提醒'
    }

    requests.post(send_url, params=params)


    def main_handler(event, context):
    return it()


    if __name__ == '__main__':
    it()

    程序能行了,可放在云函数上不行,请问怎么让云函数能读写文件,或者让程序不需要创建文件就能执行
    13 条回复    2021-06-25 16:18:29 +08:00
    l4ever
        1
    l4ever  
       2021-06-24 19:22:56 +08:00   ❤️ 1
    ihipop
        2
    ihipop  
       2021-06-24 19:29:03 +08:00 via Android
    一般云函数都有个可写的空间有限的 tmp 目录的,文件不大的话写那里面就行了。
    no1xsyzy
        3
    no1xsyzy  
       2021-06-24 19:29:47 +08:00
    请学 Python
    graetdk
        4
    graetdk  
       2021-06-24 19:32:08 +08:00
    腾讯云云函数的可写目录是 /tmp/,你要写的话需要在这个目录里写
    l4ever
        5
    l4ever  
       2021-06-24 19:47:54 +08:00   ❤️ 1
    zhuangzhelia9908
        6
    zhuangzhelia9908  
    OP
       2021-06-24 19:49:20 +08:00
    @l4ever 感谢
    zhuangzhelia9908
        7
    zhuangzhelia9908  
    OP
       2021-06-24 19:50:24 +08:00
    @graetdk 我找不到>﹏<
    zhuangzhelia9908
        8
    zhuangzhelia9908  
    OP
       2021-06-24 19:50:52 +08:00
    @graetdk 麻烦发个具体链接或者图片链接,谢谢
    cdlnls
        9
    cdlnls  
       2021-06-24 19:58:52 +08:00   ❤️ 1
    上面说的可写目录是 /tmp/ ,你打开文件的时候就这样:f = open("/tmp/hello.txt","w"),加上具体的路径 /tmp 就可以了。
    zhuangzhelia9908
        10
    zhuangzhelia9908  
    OP
       2021-06-24 20:05:32 +08:00
    @cdlnls 行了,太感谢你了
    zhuangzhelia9908
        11
    zhuangzhelia9908  
    OP
       2021-06-24 20:18:13 +08:00
    @l4ever 这里只需要 session,怎么自动提取它
    no1xsyzy
        12
    no1xsyzy  
       2021-06-25 12:09:43 +08:00
    你把一个字符串写入一个文件,再从同一个文件把内容读出来,属于如蜜传如蜜

    按 #1 的格式化,去掉 L13-L15,L21-L22,然后把 L23 降一级缩进,前面加一行 text=res 就行了
    反正处理完了文件也会被删,云函数嘛,用过就丢了,存文件完全没必要。
    zhuangzhelia9908
        13
    zhuangzhelia9908  
    OP
       2021-06-25 16:18:29 +08:00
    @no1xsyzy 知道了,感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1195 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:24 · PVG 02:24 · LAX 11:24 · JFK 14:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.