我有一个将文件下载到文件夹中的脚本。脚本工作正常,但脚本的下载将进入我的项目文件夹。我想做的是将这些下载放入另一个位置:L:\Data\Scores\ACT
我应该在脚本中的什么位置包含或更改它?我需要定义路径在哪里,或者以某种方式改变 DOWNLOAD_DIR 吗?
我正在使用 VS Code。
import requests
import json
import os
import re
from urllib.parse import urlparse, unquote
from pathlib import Path
# SET THE API URL
URL = "https://api.datalab.nrccua.org/v1"
# SET YOUR API KEY
API_KEY = "xxxxx"
# SET YOUR ORG UID
ORGANIZATION_UID = "xxxxx"
# override this if you want
DOWNLOAD_DIR = os.path.dirname(__file__)
DOWNLOAD_DIR = Path(DOWNLOAD_DIR)
if len(files_to_download) == 0:
print(f"No files to download!")
else:
for file in files_to_download:
parsed_url = urlparse(file)
\# get the file name from the url, unescape it, and then replace whitespace with underscore
escaped_filename = get_valid_filename(unquote(os.path.basename(parsed_url.path)))
download_path = DOWNLOAD_DIR / escaped_filename
print(f"Downloading file from url {file}")
\# don't use the session here
download_file_response = requests.get(file, allow_redirects=True, stream=True)
if download_file_response.ok:
print(f"Writing file to {download_path}.")
with open(download_path, "wb") as f:
\# we are going to chunk the download because we don't know how large the files are
for chunk in download_file_response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
else:
print(f"There was an error retrieving {file} with status code {download_file_response.status_code}.")
print(f"{download_file_response.content}")
# override this if you want
DOWNLOAD_DIR = os.path.dirname(__file__)
DOWNLOAD_DIR = Path(DOWNLOAD_DIR)
如评论所述,需要修改DOWNLOAD_DIR中的路径内容。
根据您提供的信息,我认为您只需要将这两行修改为
DOWNLOAD_DIR = Path('L:/Data/Scores/ACT')
。