这个Python代码有什么问题?许可错误:[错误号13]
这个程序应该能够从http:URL下载内容,并将其保存到用户选择的目录中这个Python代码有什么问题?许可错误:[错误号13],python,permissions,Python,Permissions,这个程序应该能够从http:URL下载内容,并将其保存到用户选择的目录中 import urllib.request import urllib.error def run_user_interface() -> None: url = _choose_url() if len(url) == 0: return else: print() save_path = _choose_save_path() i
import urllib.request
import urllib.error
def run_user_interface() -> None:
url = _choose_url()
if len(url) == 0:
return
else:
print()
save_path = _choose_save_path()
if len(save_path) == 0:
return
else:
_download_url(url, save_path)
def _choose_url() -> str:
print('Choose a URL to download (press Enter or Return to quit)')
return input('URL: ').strip()
def _choose_save_path() -> str:
print('Choose where you\'d like to save the file you download')
return input('Path: ').strip()
def _download_url(url_to_download: str, file_path: str) -> None:
response = None
file_to_save = None
try:
response = urllib.request.urlopen(url_to_download)
file_to_save = open(file_path, 'wb')
file_to_save.write(response.read())
except urllib.error.HTTPError as e:
print('Failed to download contents of URL')
print('Status code: {}'.format(e.code))
print()
finally:
if file_to_save != None:
file_to_save.close()
if response != None:
response.close()
if __name__ == '__main__':
run_user_interface()
在我朋友的电脑上执行这个程序效果很好。但是,当我在我的计算机上执行它时,我收到以下错误
Traceback (most recent call last):
File "C:\Users\garci\Desktop\ICS_32_Winter_2020\new_download_file.py", line 63, in <module>
run_user_interface()
File "C:\Users\garci\Desktop\ICS_32_Winter_2020\new_download_file.py", line 20, in run_user_interface
_download_url(url, save_path)
File "C:\Users\garci\Desktop\ICS_32_Winter_2020\new_download_file.py", line 40, in _download_url
file_to_save = open(file_path, 'wb')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\garci\\Desktop\\ICS_32_Winter_2020'
如何修复此权限错误?谢谢。您的程序似乎不允许写入PC上的此特定位置 更改文件夹C:\Users\garci\Desktop\ICS\u 32\u Winter\u 2020的权限,您的程序将正常运行
在Windows上执行此操作的一种简单方法是在文件资源管理器中查找目录,右键单击文件夹,选择“属性”,然后打开“安全”选项卡。从那里,您可以修改文件夹的读/写权限。发生这种情况的原因有三个: 正在写入文件时正在使用该文件。要解决此问题,请关闭该文件 除非您自己这样做,否则您正在写入的目录不太可能有写保护。 没有向python授予足够的权限来读取/写入文件。要克服此问题,请使用Windows键+X,然后按A,这将打开一个带有管理员权限的cmd promt。更改目录并尝试运行脚本。
以管理员身份运行python代码