SoFunction
Updated on 2025-05-18

How to download network files to local specified folders in Python

Downloading files in Python to a local designated folder can be achieved through the following steps, usingrequestsThe library handles HTTP requests and combinesosModule processing file path:

import requests, os,datetime
from  import urlparse,parse_qs
 
"""
 Get request header information, set cookies by yourself according to the URL
 """
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7;application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Cookie': '132213213213213213',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'
}
 
 
def download_file(url, save_dir=None,file_name=None):
    """
     Download the file and save it to the specified path, get the file name from the network path, for example https://127.0.0.1:8000/web/file/
     :param url: file download URL
     :param save_dir: file save path
     :param file_name: file name
     """
    try:
        if file_name is None:
            parse_url=urlparse(url)
            file_name=(parse_url.path)
            if not file_name:
                file_name="download_file_"+().strftime("%Y%m%d%H%M%S")
        if save_dir is None:
            save_dir=rf"C:\Users\Desktop\download_file"
        save_path=(save_dir)
        file_path=(save_path,file_name)
 
        if save_dir and not (save_dir):
            (save_dir, exist_ok=True)
 
        # Initiate a GET request with streaming        with (url, stream=True,headers=headers) as response:
            response.raise_for_status()  # Check HTTP status code 
            # Write files in chunks            with open(file_path, 'wb') as file:
                for chunk in response.iter_content(chunk_size=8192):
                    if chunk:  # Filter blank blocks that keep connected                        (chunk)
 
        print(f"File download successfully,Save the path:{file_path}")
        return True
    except  as e:
        print(f"Network request failed:{str(e)}")
    except IOError as e:
        print(f"File operation failed:{str(e)}")
    except Exception as e:
        print(f"Unknown error:{str(e)}")
 
    return False
 
 
def download_file2(url, save_dir=None,file_name=None):
    """
         Download the file and save it to the specified path, get the file name from the network path, for example https://127.0.0.1:8000/web/file/
         :param url: file download URL
         :param save_dir: file save path
         :param file_name: file name
     """
    try:
        if file_name is None:
            parse_url = urlparse(url)
            file_name = (parse_url.path)
            if not file_name:
                file_name = "download_file_" + ().strftime("%Y%m%d%H%M%S")
        if save_dir is None:
            save_dir = rf"C:\Users\Desktop\download_file"
        save_path = (save_dir)
        file_path = (save_path, file_name)
 
        if save_dir and not (save_dir):
            (save_dir, exist_ok=True)
 
        response = (url, stream=True, headers=headers)
        response.raise_for_status()
        with open(file_path, 'wb') as file:
            ()
        return True
    except  as e:
        print(f"Network request failed:{str(e)}")
    except IOError as e:
        print(f"File operation failed:{str(e)}")
    except Exception as e:
        print(f"Unknown error:{str(e)}")
 
    return False
 
 
def download_file3(url, save_dir=None,file_name=None):
    """
        Download the file and save it to the specified path,From the network pathqueryGet the file name in the parameters,For examplehttps://127.0.0.1:8000/web/file?path=2025041616372016\\
        :param url: File downloadURL
        :param save_dir: 文件Save the path
        :param file_name: file name
    """
    try:
        if file_name is None:
            parse_url = urlparse(url)
            # Extract file name from query parameter            query_params = parse_qs(parse_url.query)
            # Get the value of the path parameter, the path can be adjusted according to the actual situation            path_value = query_params.get('path', [''])[0]
 
            # Extract file name (part after the last backslash)            file_name = path_value.split('\\')[-1]
            if not file_name:
                file_name = "download_file_" + ().strftime("%Y%m%d%H%M%S")
        if save_dir is None:
            save_dir = rf"C:\Users\Desktop\download_file"
        save_path = (save_dir)
        file_path = (save_path, file_name)
 
        if save_dir and not (save_dir):
            (save_dir, exist_ok=True)
 
        response = (url, stream=True, headers=headers)
        response.raise_for_status()
        with open(file_path, 'wb') as file:
            ()
        return True
    except  as e:
        print(f"Network request failed:{str(e)}")
    except IOError as e:
        print(f"File operation failed:{str(e)}")
    except Exception as e:
        print(f"Unknown error:{str(e)}")
 
    return False

Example of usage

# Example 1: Automatically extract file names from URLsdownload_file(
    url="/",
    save_dir="./downloads"
)
 
download_file2(
    url="/",
    save_dir="./downloads"
)
 
# Example 2: Custom file namedownload_file(
    url="/data?format=csv",
    save_dir="./data_files",
    file_name=""
)
 
download_file2(
    url="/data?format=csv",
    save_dir="./data_files",
    file_name=""
)
 
# Example 3: Get file name from query parameter in network pathdownload_file3(
    url="/data?path=20250417\\",
    save_dir="./data_files",
    file_name=""
)

Key points description

File name processing:

By default, the file name (such as /extract) is extracted from the URL path.

If the URL does not contain a file name (such as ending in /), the default name is downloaded_file.

Supports customizing file names through parameter file_name.

Supports getting file names from Query parameters of URL path (such as /data?path=20250417\\extract)

Path processing:

Use modules to ensure paths are cross-platform compatible.

Automatically create the target directory (if not exists).

Streaming download:

Use stream=True to download in chunks to avoid large files from consuming too much memory.

Iter_content is written block by block to improve reliability.

Exception handling:

Catch common errors (such as network problems, insufficient permissions).

Use response.raise_for_status() to check the HTTP status code.

Scalability:

Supports custom request headers (such as emulating browser access).

chunk_size can be adjusted to optimize download speed and memory usage.

Method supplement

Python download file to specified folder

# coding:utf-8
import os
import shutil
import sys
reload(sys)
('utf8')
# print ()
# There are many folders under some folders, and there are many video files under each folder. Now, through scripts, transfer all files under the folder to a directory.# Statistics the number of folders and files to be accessedcountNum = [0, ]countFile = [0, ]# Select Remove All or specify the suffix file# Find filesdef move_all_files(dir_path):
if (dir_path):
countNum[0] += 1
# Output number of folders traversedprint "*****", countNum[0], "*****"+dir_path
# All files and folders in the specified folderpath_list = (dir_path)
#Travelfor each_path in path_list:
# If it is a folder, continue to traverseprint each_path
if (dir_path+""+each_path):
# Move all files to the specified directorysrc=dir_path+""+each_path
move_all_files(src)
else:
# If the file type is specified, copy the filefile_type = (each_path)[1] # Determine whether it is the selected file typeselected = False
if file_type == select_type or select_type == 'All':
selected = True
if selected:
# Copy the filesrc_file = dir_path + "" + each_path
des_file = des_pos + "" + each_path
print "Copying", each_path
(src_file, des_file)
# File +1countFile[0] += 1
else:
print "The specified path does not exist"
# The folder location where the file needs to be copiedgive_pos = r"C:UserslanceDownloadsJava Web Programming Related"
# Locations to which you need to copydes_pos = r"C:UserslanceDownloads test"
# All or Specify the file suffix nameselect_type = 'All'
# If it does not exist, createif not (unicode(des_pos, 'utf-8')):
(unicode(des_pos, "utf-8"))
# Move filesmove_all_files(unicode(give_pos, "utf-8"))
print "Transfer files from ****'", give_pos, "'Copy to ****'", des_pos, "'"
print "A total of visits", countNum[0], "Folders"
print "Copyed in total", countFile[0], "A file"

This is the article about how to download network files in python to local designated folders. For more related python download network files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!