I am using os.path.join and os.mkdir to create folders on a loop.I would like to create a folder with the city name/inputs/b-d.
However, returns error. Similar behavior to this:
import shutil, os
PATH_TEST=r'C:\Users\Ricardo\Dropbox\test'
d = {'Municipalities': ['NY', 'Boston']}
df = pd.DataFrame(data=d)
df
for index in range(len(df)):
aaa = os.path.join(PATH_TEST,df['Municipalities'][index],'inputs',"b-d")
if not os.path.exists(aaa):
os.mkdir(aaa)
Error:
FileNotFoundError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_23524\2121887104.py in <cell line: 6>()
7 aaa = os.path.join(PATH_TEST,df['Municipalities'][index],'inputs',"b-d")
8 if not os.path.exists(aaa):
----> 9 os.mkdir(aaa)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\Ricardo\\Dropbox\\test\\NY\\inputs\\b-d'
CodePudding user response:
I would need to see your file system to answer fully but it seems likely that you're trying to create multiple folders e.g. NY, inputs and b-d which os.mkdir will not handle.
Instead I recommend swapping out os.mkdir with os.makedirs.
CodePudding user response:
Most probable issue here is you are trying to create folder inside a folder that does not exist.
What do I mean? Your desired folder structure is like this city_name/inputs/b-d
. When you check if it exists, it checks if b-d
folder exists. If it doesn't it tries to create it.
Problem is that you are trying to create it inside folder inputs
that also doesn't exist and that is why you get an error. NOTE: this would also happen if a parent folder city_name
doesn't exist.
To fix this you have to approach this iteratively according to the folder structure. First create city_name
then inputs
and then b-d
.