我目前正在尝试编写一个Python脚本,用于创建ArcGIS Online图层的备份,但我一直收到WinError 32与此代码和基本上任何脚本我运行。我以前遇到过这个问题,所以我认为制作一个临时目录会有帮助,但问题仍然发生,虽然我对目录的工作原理没有信心,所以我可能只是在输入不相关的代码。我目前使用的代码如下(还请注意,我对编码非常非常陌生):
# create a temporary directory since I keep getting a file in use error but this also doesnt fix it lol
import tempfile
with tempfile.TemporaryDirectory() as tmpdir:
print('created temporary directory', tmpdir)
# Import necessary modules
import arcgis
from arcgis.gis import GIS
# Create a GIS object
gis = GIS("insert_link", "username", "password")
# Get the item that represents the layer that you want to copy
layer_item = gis.content.get("layer_ID")
# Create a copy of the layer
copy_item = layer_item.copy()
# Find any old copies of the layer and delete them
for item in gis.content.search(query="title:'Copy of {}'".format(layer_item.title)):
if item.type == "Feature Layer":
item.delete()
# Save the new copy of the layer
copy_item.save()
# one day I will see this
print("File Downloaded")
这是我得到的错误:
C:\Users\SeanW\anaconda3\envs\MeganScriptTests\python.exe C:\Users\SeanW\PycharmProjects\AGOLBackup3\main.py
created temporary directory C:\Users\SeanW\AppData\Local\Temp\tmpj0yvbg64
Traceback (most recent call last):
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\importlib\_common.py", line 92, in _tempfile
os.write(fd, reader())
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\importlib\abc.py", line 371, in read_bytes
with self.open('rb') as strm:
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\importlib\_adapters.py", line 54, in open
raise ValueError()
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\SeanW\PycharmProjects\AGOLBackup3\main.py", line 7, in <module>
import arcgis
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\arcgis\__init__.py", line 3, in <module>
from arcgis.auth.tools import LazyLoader
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\arcgis\auth\__init__.py", line 1, in <module>
from .api import EsriSession
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\arcgis\auth\api.py", line 11, in <module>
certifi_win32.wincerts.verify_combined_pem()
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\certifi_win32\wincerts.py", line 65, in verify_combined_pem
with open(certifi_pem()) as certifi_pem_handle:
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\certifi_win32\wincerts.py", line 52, in certifi_pem
import certifi
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\wrapt\importer.py", line 177, in _exec_module
notify_module_loaded(module)
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\wrapt\decorators.py", line 470, in _synchronized
return wrapped(*args, **kwargs)
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\wrapt\importer.py", line 136, in notify_module_loaded
hook(module)
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\certifi_win32\wrapt_certifi.py", line 20, in apply_patches
certifi_win32.wincerts.CERTIFI_PEM = certifi.where()
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\site-packages\certifi\core.py", line 72, in where
_CACERT_PATH = str(_CACERT_CTX.__enter__())
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\contextlib.py", line 135, in __enter__
return next(self.gen)
File "C:\Users\SeanW\anaconda3\envs\MeganScriptTests\lib\importlib\_common.py", line 98, in _tempfile
_os_remove(raw_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\SeanW\\AppData\\Local\\Temp\\tmpx9og0v3t'
Process finished with exit code 1
我也试过python 3.11,但是它给了我另一个错误,它不能打开一个孤儿路径,我在网上找不到任何关于这个的信息。我也在另一台计算机上试过这个,以防我的python配置错误,同样的问题发生。我读过它可能是python-cereri-win32包,但是当我试图卸载它时,同样的错误发生。在创建临时目录后缩进代码也不能解决winerror 32问题。
1条答案
按热度按时间fdbelqdn1#
您正在使用
tempfile.TemporaryDirectory
作为上下文管理器(使用with
):当以这种方式使用时,目录会在缩进块的末尾自动删除:
在完成临时目录对象的上下文或销毁时,从文件系统中移除新创建的临时目录及其所有内容。
无论您想在临时目录中做什么,都需要在缩进的块中进行。