OS 错误 22:在 Azure 上打开和关闭文件 "Invalid Argument"
OS error 22 : File open and close on Azure "Invalid Argument"
运行 下面 Azure Blobstorage 中的代码同时抛出
OS 错误 22:Invalid 参数指向 f.close()
与 open() 一起使用时是否使用 Close() 导致 OS 错误 22 问题?
了解不需要 Close() 但想了解 OS 错误 22
的根本原因
*with open(openfilepath,"w+") as f:
f.write(writeFile )
f.close()*
检查以下可能性并尝试。
1
如果要使用 f.close(),您可以尝试不使用 with
f = open(file_name, 'w+') # open file in write mode
f.write('write content')
f.close()
但是
处理文件对象时,最好使用 with 关键字。优点是文件在其套件完成后会正确关闭。一旦 Python 从“with”块中退出,文件就会自动关闭。
所以请删除 f.close() 并尝试。
with open(file_name, 'w+') as f :
f.write('write content')
参考this了解更多信息。
2
如果上述方法不起作用,请检查文件路径:这可能是由于文件路径名中存在一些无效字符:它不应包含少数特殊字符。
查看文件路径是否为例如:“dbfs:/mnt/data/output/file_name.xlsx”
检查 dbfs 之前是否有“/”(例如 /dbfs:/mnt/…)。如果存在,请尝试删除。
NOTE:
``r+'': Open for reading and writing. The stream is positioned at
the beginning of the file.
``w+'': Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned at the
beginning of the file.
``a+'' : Open for reading and writing. The file is created if it does
not exist. The stream is positioned at the end of the file. Subse-
quent writes to the file will always end up at the then current end of
file, irrespective of any intervening fseek(3) or similar.
因此,如果 w+ 不是必需的,请尝试使用其他模式,例如 r+。参见 Python documentation
删除 F.close() 解决了使用 With
时的问题
运行 下面 Azure Blobstorage 中的代码同时抛出 OS 错误 22:Invalid 参数指向 f.close()
与 open() 一起使用时是否使用 Close() 导致 OS 错误 22 问题? 了解不需要 Close() 但想了解 OS 错误 22
的根本原因*with open(openfilepath,"w+") as f:
f.write(writeFile )
f.close()*
检查以下可能性并尝试。
1
如果要使用 f.close(),您可以尝试不使用 with
f = open(file_name, 'w+') # open file in write mode
f.write('write content')
f.close()
但是
处理文件对象时,最好使用 with 关键字。优点是文件在其套件完成后会正确关闭。一旦 Python 从“with”块中退出,文件就会自动关闭。
所以请删除 f.close() 并尝试。
with open(file_name, 'w+') as f :
f.write('write content')
参考this了解更多信息。
2
如果上述方法不起作用,请检查文件路径:这可能是由于文件路径名中存在一些无效字符:它不应包含少数特殊字符。 查看文件路径是否为例如:“dbfs:/mnt/data/output/file_name.xlsx” 检查 dbfs 之前是否有“/”(例如 /dbfs:/mnt/…)。如果存在,请尝试删除。
NOTE: ``r+'': Open for reading and writing. The stream is positioned at the beginning of the file.
``w+'': Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
``a+'' : Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subse- quent writes to the file will always end up at the then current end of file, irrespective of any intervening fseek(3) or similar.
因此,如果 w+ 不是必需的,请尝试使用其他模式,例如 r+。参见 Python documentation
删除 F.close() 解决了使用 With
时的问题