Home > OS >  how to solve Error during sub directory in python
how to solve Error during sub directory in python

Time:05-11

path = '\2B-Data\'

Welcome Vishal Sharma. We are preparing data of 2B for you. Traceback (most recent call last): File "c:\Users\Vishal Sharma\Desktop\New folder (5)\2B.py", line 152, in
for a,i in enumerate(os.listdir(path)): FileNotFoundError: [WinError 3] The system cannot find the path specified: '\2B-Data\' PS C:\Users\Vishal Sharma\Desktop\New folder (5)>

CodePudding user response:

for paths in python you have to prefix it with 'r' something like

path = r'\2B-Data\'

An ‘r’ preceding a string denotes a raw, (almost) un-escaped string.

The escape character is backslash, that is why a normal string will not work as a Windows path string.

CodePudding user response:

try this for example:

import os
os.makedirs('folder/2B-Data', exist_ok=True)

CodePudding user response:

You can also got for something like this, if you want to go for the detailed way:

# importing os module
import os
 
# os.makedirs() method will raise an OSError if directory to be created already exists
# suppressed by setting exist_ok as True
    
# directory
directory = "2B-Data"
 
# parent directory path
parent_dir = "/someDirectory/subDir"
 
# path
path = os.path.join(parent_dir, directory)
 
# create the directory
try:
    os.makedirs(path, exist_ok = True)
    print("Directory '%s' created successfully" %directory)
except OSError as error:
    print("Directory '%s' can not be created")
  • Related