要更改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變量來指定下載文件的路徑。