要更改Python下载文件的路径,可以使用以下方法:
import os
from urllib.request import urlretrieve
url = 'https://example.com/file.txt'
download_path = 'C:/Users/username/Downloads/'
os.chdir(download_path)
urlretrieve(url, 'file.txt')
import requests
url = 'https://example.com/file.txt'
download_path = 'C:/Users/username/Downloads/'
response = requests.get(url)
with open(download_path + 'file.txt', 'wb') as file:
file.write(response.content)
无论哪种方法,都可以通过更改download_path变量来指定下载文件的路径。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python中collect是什么函数