0

I am using the following code:

filename = r'C:/Users/Automated Analysis.ipynb'
dest = r'C:/Users/Automated Analysis.py'
os.system("ipynb-py-convert %s %s" % (filename, dest))

But it give me this error:

    raise(Exception('Extensions must be .ipynb and .py or vice versa'))
Exception: Extensions must be .ipynb and .py or vice versa

I am not sure how to make my code above work.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Zanam
  • 4,607
  • 13
  • 67
  • 143

1 Answers1

1

It's because you have spaces for your filenames.

Your os.system command becomes:

ipynb-py-convert C:/Users/Automated Analysis.ipynb C:/Users/Automated Analysis.py

...and as you can see, there are now 4 inputs. Running that directly on the command line will produce the same Exception message. It is trying to convert C:/Users/Automated into Analysis.ipynb.

Just wrap the filenames in quotes:

os.system("ipynb-py-convert '%s' '%s'" % (filename, dest))

Or you can use subprocess.run:

import subprocess

filename = r'C:/Users/Automated Analysis.ipynb'
dest = r'C:/Users/Automated Analysis.py'
subprocess.run(['ipynb-py-convert', filename, dest])

See Advantages of subprocess over os.system and Replacing os.system().

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135