Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

TypeError: expected str, bytes or os.PathLike object, not int in Python

Verified FixPython 3.10+Python NativeSilo: core

Quick Fix / Solution Rapide

Explicitly convert numbers or objects to string using str(val) or use f-string interpolation f'{val}' before string operations.

Root Cause Analysis

This error occurs when Python executes a function, string method (like str.join()), or filesystem path utility (like open(), os.path.join()) that strictly requires a string (str), bytes (bytes), or os.PathLike object, but receives a different data type such as int, list, dict, or None.

1. Joining Non-String Iterables with str.join()

The str.join() method requires every element in the input sequence to be a string. Passing a list containing integers (e.g. ['log', 2026]) raises TypeError: sequence item 1: expected str instance, int found.

2. Passing Integers or None to Path Utilities

Functions like open(file_id) or os.path.exists(user_input) expect a valid string or Path object. Passing an unvalidated integer ID or None triggers TypeError: expected str, bytes or os.PathLike object, not int.

3. String Concatenation with + Operator

In Python, writing 'User ID: ' + user_id when user_id is an integer raises TypeError: can only concatenate str (not 'int') to str.

4. Missing Argument Type Validation in APIs

JSON payloads parsed into Python dictionaries contain native integer values (e.g. {'port': 8080}). Passing them directly into string formatting functions raises errors.

Reproduction Code (MCVE)

Example: Bug Reproduction
# Passing a list containing integers to str.join() raises TypeError
path_elements = ['var', 'log', 'app', 2026]
formatted_path = '/'.join(path_elements)

Solution 1: Convert Elements to Strings with Generator Expression

Use str(item) inside a generator expression or list comprehension before calling .join().

Example: Recommended Solution
path_elements = ['var', 'log', 'app', 2026]
# Convert all items to string
formatted_path = '/'.join(str(item) for item in path_elements)
print(f'Successfully formatted path: {formatted_path}')

Solution 2: Use F-Strings for Safe Formatting

Use Python f-strings which automatically call __format__ / __str__ on any primitive or object.

Example: Alternative Solution
status_code = 404
endpoint = '/api/v1/users'
# F-strings safely handle integers, booleans, and floats
log_message = f'Request to {endpoint} returned status {status_code}'
print(log_message)

A common mistake is trying to convert None with str(val). While str(None) produces the string 'None', this often results in generating invalid filenames like 'report_None.csv'. Always check if val is not None: before converting. Edge cases occur with Boolean values: in Python, bool is a subclass of int (isinstance(True, int) is True). Converting booleans produces 'True' / 'False'. Contrast this error with ValueError: invalid literal for int() with base 10, which occurs during the inverse operation (parsing strings to numbers).