python - 出现错误 : [Errno 95] Operation not supported while writing zip file in databricks

标签 python python-3.x azure zip databricks

在这里,我尝试使用 Databricks 中的以下代码压缩文件并将其写入一个文件夹(挂载点)。

# List all files which need to be compressed
import os
modelPath  = '/dbfs/mnt/temp/zip/'
filenames = [os.path.join(root, name) for root, dirs, files in os.walk(top=modelPath , topdown=False) for name in files]
print(filenames)

zipPath = '/dbfs/mnt/temp/compressed/demo.zip'
import zipfile
with zipfile.ZipFile(zipPath, 'w') as myzip:
  for filename in filenames:
    print(filename)
    print(myzip)
    myzip.write(filename)

但我收到错误 [Errno 95] 操作不受支持。

错误详细信息

OSError                                   Traceback (most recent call last)
<command-2086761864237851> in <module>
     15     print(myzip)
---> 16     myzip.write(filename)

/usr/lib/python3.8/zipfile.py in write(self, filename, arcname, compress_type, compresslevel)
   1775             with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
-> 1776                 shutil.copyfileobj(src, dest, 1024*8)
   1777 

/usr/lib/python3.8/zipfile.py in close(self)
   1181                 self._fileobj.write(self._zinfo.FileHeader(self._zip64))
-> 1182                 self._fileobj.seek(self._zipfile.start_dir)
   1183 

OSError: [Errno 95] Operation not supported

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
/usr/lib/python3.8/zipfile.py in close(self)
   1837                     if self._seekable:
-> 1838                         self.fp.seek(self.start_dir)
   1839                     self._write_end_record()

OSError: [Errno 95] Operation not supported

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
OSError: [Errno 95] Operation not supported

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<command-2086761864237851> in <module>
     14     print(filename)
     15     print(myzip)
---> 16     myzip.write(filename)

/usr/lib/python3.8/zipfile.py in __exit__(self, type, value, traceback)
   1310 
   1311     def __exit__(self, type, value, traceback):
-> 1312         self.close()
   1313 
   1314     def __repr__(self):

/usr/lib/python3.8/zipfile.py in close(self)
   1841             fp = self.fp
   1842             self.fp = None
-> 1843             self._fpclose(fp)
   1844 
   1845     def _write_end_record(self):

/usr/lib/python3.8/zipfile.py in _fpclose(self, fp)
   1951         self._fileRefCnt -= 1
   1952         if not self._fileRefCnt and not self._filePassed:
-> 1953             fp.close()
   1954 
   1955 

谁能帮我解决这个问题。

注意:这里我可以使用shutil压缩文件,但我想避免驱动程序,所以使用上面的方法。

最佳答案

您没有提供装载的详细信息,可能是 Blob 存储或 ADLSv2,并且显然它不允许文件查找。

看看这个简单的代码片段:

%python

path = '/dbfs/mnt/temp/testfile'

with open(path, "w") as f:
    f.write("test")
    f.seek(1)
    f.write("x")

with open(path, "r") as f:
    print(f.read()) 

它将在 f.seek(1) 处抛出“不支持操作”。

path = '/tmp/testfile' 重复相同的操作,您将获得正确的结果(“txst”)。

奇怪的是,根本不应该到达 zipfile.py 中的搜索,看起来 self._seekable 返回了错误的值,我不确定这是库的问题还是 azure 。

无论如何,只需在本地目录中创建存档,然后将其移动到挂载即可。

tempPath = '/tmp/demo.zip'
zipPath = '/dbfs/mnt/temp/compressed/demo.zip'
import zipfile
import os

with zipfile.ZipFile(tempPath, 'w') as myzip:
  for filename in filenames:
    print(filename)
    print(myzip)
    myzip.write(filename)

os.rename(tempPath, zipPath)

关于python - 出现错误 : [Errno 95] Operation not supported while writing zip file in databricks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75853317/

相关文章:

python - 在 Transcrypt 中使用 XMLHttpRequest()

Python 3.2.1 : exec ('x = y()' ) sets a value in a toy example, 但不在完整代码中

python - 计算圆和直线的变换

python-3.x - request.args 中的flask 可选参数

android - 我想计算从 azure 表返回的行数

python - 在 Python 中对连续日期进行分组

python-3.x - 使用 openpyxl 将图像插入 Excel

python - 我应该返回无还是(无,无)?

sql-server - 如何从azure网站连接aws VM上的数据库

azure - Azure 数据工厂复制事件使用什么协议(protocol)?