Here is an alternative way to do this using the excellent standard library pathlib module, which generally makes things neater.
As explained elsewhere, to_csv will create the file if it doesn't exist, but won't create any non-existent directories in the path to the file, so you need to first ensure that these exist.
from pathlib import Path
output_file = 'my_file.csv'
output_dir = Path('long_path/to/my_dir')
output_dir.mkdir(parents=True, exist_ok=True)
df.to_csv(output_dir / output_file) # can join path elements with / operator
Setting parents=True will also create any necessary parent directories, and exist_ok=True means it won't raise an error if the directory already exists, so you don't have to explicitly check that separately.