2022年 11月 9日

python中tmp的用法_使用python写入AWS Lambda中的/ tmp目录 – python

目标

我正在尝试将zip文件写入python aws lambda中的/ tmp文件夹中,因此我可以在压缩之前提取操作,并将其放入s3存储桶中。

问题

Os Errno30只读文件系统

在我的计算机上对该代码进行了本地测试,以确保在将文件上传到AWS之前,该文件将写入我的工作目录。这是我要使用的代码。

file = downloadFile() #This is api call that returns binary zip object

newFile = open(‘/tmp/myZip.zip’,’wb’)

newFile.write(file)

extractAll(‘/tmp/myZip.zip’)

这是试图提取压缩文件的代码

def extractAll(self,source):

with zipfile.ZipFile(source, ‘r’) as archive:

archive.extractall()

这是痕迹

[Errno 30] Read-only file system: ‘/var/task/test-deploy’: OSError

Traceback (most recent call last):

File “/var/task/web.py”, line 31, in web

performAction(bb, eventBody)

File “/var/task/src/api/web.py”, line 42, in performAction

zipHelper.extractAll(‘/tmp/myZip.zip’)

File “/var/task/src/shared/utils/zipfilehelper.py”, line 24, in extractAll

archive.extractall()

File “/var/lang/lib/python3.6/zipfile.py”, line 1491, in extractall

self.extract(zipinfo, path, pwd)

File “/var/lang/lib/python3.6/zipfile.py”, line 1479, in extract

return self._extract_member(member, path, pwd)

File “/var/lang/lib/python3.6/zipfile.py”, line 1538, in _extract_member

os.mkdir(targetpath)

OSError: [Errno 30] Read-only file system: ‘/var/task/test-deploy’

参考方案

extractAll()将提取当前目录中的文件,在您的情况下为/var/task/test-deploy。

您需要使用os.chdir()更改当前目录:

import os, zipfile

os.chdir(‘/tmp’)

with zipfile.ZipFile(source, ‘r’) as archive:

archive.extractall()

更好的是,您可以create a temporary directory并在那里提取文件,以避免污染/tmp:

import os, tempfile, zipfile

with tempfile.TemporaryDirectory() as tmpdir:

os.chdir(tmpdir)

with zipfile.ZipFile(source, ‘r’) as archive:

archive.extractall()

如果要在提取文件后还原当前工作目录,请考虑使用此装饰器:

import os, tempfile, zipfile, contextlib

@contextlib.context_manager

def temporary_work_dir():

old_work_dir = os.getcwd()

with tempfile.TemporaryDirectory() as tmp_dir:

os.chdir(tmp_dir)

try:

yield

finally:

os.chdir(old_work_dir)

with temporary_work_dir():

with zipfile.ZipFile(source, ‘r’) as archive:

archive.extractall()

在返回’Response'(Python)中传递多个参数 – python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get(“api/agent/applicationaware”).subscribe((data:any)… python文件:def get(request): … return Response(seriali…Python exchangelib在子文件夹中读取邮件 – python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…R’relaimpo’软件包的Python端口 – python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。Python ThreadPoolExecutor抑制异常 – python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print(‘In div_zero’) return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…如何用’-‘解析字符串到节点js本地脚本? – python

我正在使用本地节点js脚本来处理字符串。我陷入了将’-‘字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require(‘yargs’) .usage(‘$0 [string]’) .argv; console.log(argv…