diff --git a/nbconvert/nbconvertapp.py b/nbconvert/nbconvertapp.py index e179c125e..d56bb68b6 100755 --- a/nbconvert/nbconvertapp.py +++ b/nbconvert/nbconvertapp.py @@ -218,7 +218,7 @@ def _classes_default(self): output_base = Unicode( "{notebook_name}", help="""Overwrite base name use for output files. - Supports pattern replacements '{notebook_name}' and '{notebook_filename}'. + Supports pattern replacements '{notebook_name}'. """, ).tag(config=True) @@ -420,9 +420,7 @@ def _notebook_filename_to_name(self, notebook_filename): """ basename = os.path.basename(notebook_filename) notebook_name = basename[: basename.rfind(".")] - notebook_name = self.output_base.format( - notebook_name=notebook_name, notebook_filename=notebook_filename - ) + notebook_name = self.output_base.format(notebook_name=notebook_name) return notebook_name @@ -513,7 +511,7 @@ def write_single_notebook(self, output, resources): raise KeyError(msg) notebook_name = resources["unique_key"] - if self.use_output_suffix: + if self.use_output_suffix and self.output_base == "{notebook_name}": notebook_name += resources.get("output_suffix", "") write_results = self.writer.write(output, resources, notebook_name=notebook_name) @@ -585,18 +583,6 @@ def convert_notebooks(self): if ext == self.exporter.file_extension: self.output_base = base - # Validate that output_base does not cause us to overwrite already generated - # files - notebook_names = [self._notebook_filename_to_name(fn) for fn in self.notebooks] - if len(notebook_names) != len(set(notebook_names)): - msg = ( - "Conversion would override an already generated output. " - "This is probably due to --output or output_base configuration " - "leading to non-unique output names. " - f"Output notebook names were: {notebook_names}" - ) - raise ValueError(msg) - # convert each notebook if not self.from_stdin: for notebook_filename in self.notebooks: diff --git a/nbconvert/tests/test_nbconvertapp.py b/nbconvert/tests/test_nbconvertapp.py index a05d59268..45deb10fa 100644 --- a/nbconvert/tests/test_nbconvertapp.py +++ b/nbconvert/tests/test_nbconvertapp.py @@ -654,16 +654,31 @@ def test_output_base(self): for nbn in notebook_names: assert os.path.isfile(f"{nbn}.md") - with pytest.raises(OSError), self.create_temp_cwd([x + ".ipynb" for x in notebook_names]): - self.nbconvert("*.ipynb --output notebook_test_name --to markdown") - # Test single output with static output name - with self.create_temp_cwd([notebook_names[0] + ".ipynb"]): - self.nbconvert("*.ipynb --output notebook_test_name --to markdown") + nbname = notebook_names[0] + with self.create_temp_cwd([nbname + ".ipynb"]): + self.nbconvert(f"{nbname}.ipynb --output notebook_test_name --to markdown") assert os.path.isfile("notebook_test_name.md") + self.nbconvert(f"{nbname}.ipynb --to notebook") + assert os.path.isfile("notebook1.nbconvert.ipynb") + + self.nbconvert(f"{nbname}.ipynb --to notebook --output out.ipynb") + assert os.path.isfile("out.ipynb") + # Test double extension fix with self.create_temp_cwd([notebook_names[0] + ".ipynb"]): self.nbconvert("*.ipynb --output notebook_test_name.md --to markdown") assert os.path.isfile("notebook_test_name.md") assert not os.path.isfile("notebook_test_name.md.md") + + def test_same_filename_different_dir(self): + """ + Check if files with same name in different directories pose a problem + """ + with self.create_temp_cwd() as temp_wd: + self.copy_files_to(["notebook1.ipynb"], temp_wd + "/dir1") + self.copy_files_to(["notebook1.ipynb"], temp_wd + "/dir2") + self.nbconvert("dir1/notebook1.ipynb dir2/notebook1.ipynb --to markdown") + assert os.path.isfile("dir1/notebook1.md") + assert os.path.isfile("dir2/notebook1.md")