Search code examples
pythonlinuxwindowsoperating-systemmkdir

os.mkdir(linux_path) does not throw error when running in Windows


In a python snippet, i've os.mkdir(linux_path) and for testing purposes, i'm running the python code in Windows.

However, even when the linux path in windows is not available, the code is running and still not creating the path (as expected). But the code should throw some error since the code is trying to create a linux path in windows and still no error.

if not os.path.exists(<Linux_path>):
    os.makedirs(<Linux_path>)

could anyone please help on how to handle the exception/error situation here.

Thanks in Advance. Arjun.


Solution

  • In windows when you use os.makedirs(r'\home\name\abc') it will consider it as windows install directory path and create folder abc at C:\home\name and if you execute command os.makedirs(r'home\name\abc') this will create folder home in directory where your python script is running and it will create sub-folders within it(home - here). Note: effect of command os.makedirs(r'\home\name\abc') and os.makedirs(r'/home/name/abc') will be the same. so you can use \ universally in your path. It is helpful while working with cross-platform.