python os. mkdir failed to create a directory

  • 2020-12-21 18:05:35
  • OfStack

The cause of

1 small problem I encountered today when creating directories using python os.mkdir:


feature_dir = os.path.join(os.getcwd(), 'system', 'feature')
if not os.path.exists(feature_dir):
 os.mkdir(feature_dir)

Results indicate error: OSError: [2] Errno No such file or directory: '/ home hyb/hyb_speech / 2 _word/applications/system/feature'

why

After checking 1 reason, it was found that the previous level directory of feature was not created either.

Specific reasons are as follows:

1.mkdir( path [,mode] )

Effect: Create 1 directory, either relative or absolute path. Default mode for mode is 0777.

If the directory has multiple levels, the last level is created. If the parent directory of the last level 1 directory does not exist, an OSError is thrown.

2.makedirs( path [,mode] )

What it does: Create a recursive directory tree, either relative or absolute. mode also has a default mode of 0777.

If the subdirectory creation fails or already exists, an exception for OSError is thrown. Error 183 on Windows is an exception that already exists for the directory. If path has only level 1, it is the same as mkdir1.

To solve

Solutions:

1. Create system first, then feature;

2. Use ES64en. makedirs(feature_dir) directly.


Related articles: