如何使用Python创建存储气象文件的新目录?附数据获取存储场景
Hey there! Automating directory creation for your weather data is super straightforward with Python's standard libraries. Let me walk you through two common approaches tailored to your use case (4 cities, 48 total files):
Method 1: Use pathlib (Python 3.4+ Recommended)
pathlib is a modern, object-oriented library that handles cross-platform path separators seamlessly, making it the cleaner choice for recent Python versions.
Basic: Create a Directory per City
If you want a dedicated folder for each city's weather station (matching your station IDs like KMDLAURE5), use this code:
from pathlib import Path # Replace with your 4 actual station IDs station_ids = ["KMDLAURE5", "KNYC123", "LAX456", "CHI789"] # Loop through each station to create directories for station_id in station_ids: # Build the full path (e.g., ./weather_data/KMDLAURE5) dir_path = Path("./weather_data") / station_id # Create directory (and parent folders if needed) without errors if it exists dir_path.mkdir(parents=True, exist_ok=True) print(f"Created/verified directory: {dir_path.resolve()}")
parents=True: Automatically creates parent directories (likeweather_data) if they don't existexist_ok=True: Skips error-throwing if the directory already exists (perfect for rerunning your scraper)
Advanced: Nested Directories by City + Year/Month
Since you have 48 files (4 cities × 12 months), you might want nested folders to organize data by year and month. Here's how:
from pathlib import Path station_ids = ["KMDLAURE5", "KNYC123", "LAX456", "CHI789"] target_year = 2018 # Adjust if you're scraping multiple years months = range(1, 13) # Covers 1-12 months for station_id in station_ids: for month in months: # Build nested path: ./weather_data/KMDLAURE5/2018/month_02 dir_path = Path("./weather_data") / station_id / str(target_year) / f"month_{month:02d}" dir_path.mkdir(parents=True, exist_ok=True)
This structure will perfectly align with the monthly data you're pulling from Wunderground.
Method 2: Use os Module (Python 2/3 Compatible)
If you need to support older Python versions, the os module works reliably:
Python 3.2+ Version
import os station_ids = ["KMDLAURE5", "KNYC123", "LAX456", "CHI789"] for station_id in station_ids: dir_path = os.path.join("./weather_data", station_id) # Create multi-level directories, no error if exists os.makedirs(dir_path, exist_ok=True) print(f"Created/verified directory: {os.path.abspath(dir_path)}")
Python 2 Compatible Version
Since exist_ok isn't available in Python 2, add a manual check:
import os station_ids = ["KMDLAURE5", "KNYC123", "LAX456", "CHI789"] for station_id in station_ids: dir_path = os.path.join("./weather_data", station_id) if not os.path.exists(dir_path): os.makedirs(dir_path) print(f"Created/verified directory: {os.path.abspath(dir_path)}")
Integrate with Your Scraper Workflow
Add the directory creation code at the start of your scraping script to ensure folders exist before saving cleaned data. Here's a quick example:
from pathlib import Path import requests # Define your target parameters station_id = "KMDLAURE5" year = 2018 month = 2 # Create the save directory first save_dir = Path("./weather_data") / station_id / str(year) / f"month_{month:02d}" save_dir.mkdir(parents=True, exist_ok=True) # Scrape data (simplified example) url = f"https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={station_id}&year={year}&month={month}&graphspan=month&format=1" response = requests.get(url) # --- Add your data cleaning logic here --- # Save cleaned data to the prepared directory save_path = save_dir / f"{station_id}_{year}_{month:02d}_cleaned.csv" with open(save_path, "w", encoding="utf-8") as f: f.write(response.text) # Replace with your cleaned data content
内容的提问来源于stack exchange,提问作者Ma_

