Downloading a picture via urllib and python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's standard-library urllib package is enough to download an image when you do not want an extra dependency such as requests. The core workflow is simple: open the URL, read the bytes, and write them to a local file in binary mode.
Basic Download with urllib.request
The low-level pattern uses urlopen and manual file writing.
This works because images are binary data. Opening the output file with "wb" is required so Python does not treat the bytes as text.
A Convenient Shortcut with urlretrieve
For simple downloads, urlretrieve is more compact.
This is convenient for small scripts, but many developers prefer urlopen because it gives more control over response handling and errors.
Add Basic Error Handling
Network code should assume failure is possible. Invalid URLs, timeouts, redirects, and permission problems can all happen.
That pattern is more realistic than a raw download because production code has to handle failure cases explicitly.
Choosing the File Name
Sometimes the URL does not contain a reliable file name or extension. In that case, choose the local name yourself rather than trying to infer too much from the URL. If the server provides a Content-Type header, you can inspect it, but saving with a known output name is often simpler.
For example:
- use a timestamped name for scraped datasets
- use a stable deterministic name for repeatable downloads
- use a temporary file if the image is only an intermediate artifact
Add Headers When a Server Expects Them
Some servers reject bare default requests or return a different response unless a user agent is present. In those cases, create a Request object and attach headers explicitly.
This does not override site policy, but it can make a legitimate scripted request behave more like a normal browser fetch when the server expects a minimal header set.
Respect the Source Site
Downloading images programmatically can move into scraping territory quickly. Before crawling many files, check the site's terms, robots guidance where relevant, and rate limits. The fact that a URL is reachable does not automatically mean bulk downloading is appropriate.
For one-off internal or tutorial cases, the technical mechanics are easy. At scale, polite request behavior matters just as much as the code.
Common Pitfalls
- Opening the output file in text mode corrupts binary image data. Always write image content with
"wb". - Ignoring HTTP and URL errors makes failures look like empty or broken files. Handle
HTTPErrorandURLErrorexplicitly. - Assuming the URL always implies a good file extension can produce confusing local files. Choose or validate the output name yourself.
- Downloading large files with
response.read()into memory may be wasteful. For bigger payloads, stream in chunks instead of reading everything at once. - Treating bulk image download as a purely technical task can cause policy or rate-limit problems. Be respectful of the source site and its usage rules.
Summary
- '
urllib.requestcan download pictures without any third-party package.' - Use
urlopenfor control orurlretrievefor a quick one-liner. - Write image data to disk in binary mode.
- Add error handling for HTTP and network failures.
- For larger or repeated downloads, think about naming, streaming, and source-site constraints.
Related reading
- Drop all duplicate rows across multiple columns in Python Pandas
- Drop columns whose name contains a specific string from pandas DataFrame
- Drop data frame columns by name
- Dropping infinite values from dataframes in pandas?
- Dump a NumPy array into a csv file
- Dump a NumPy array into a csv file
- Duplicate log output when using Python logging module
- Dynamic instantiation from string name of a class in dynamically imported module?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.