Home > Enterprise >  How to create a Subdirectory with os.join.path in python?
How to create a Subdirectory with os.join.path in python?

Time:11-04

So here is my issue:

datasheet_path = os.path.join(self._output_path, datasheet_name)

I am trying to get the datasheet to be in a subfolder called datasheets, but the current implementation only allows me to get the datasheets in the ._output_path, which is the folder that I select.

What I really want is to be able to have something like: output_path_folder/datasheets_folder/datasheet_name.

Any help would be greatly appreciated

CodePudding user response:

os.path.join can take more than 2 path components, so this works :

os.path.join(self._output_path, "datasheets_folder", datasheet_name)

CodePudding user response:

I would suggest the standard python library pathlib for working with paths.

from pathlib import Path

datasheet_path = Path(self._output_path) / datasheets_folder / datasheet_name
datasheet_path.parent.mkdir(parents=True, exist_ok=True)
  • Related