Reputation: 115
I am trying to create a number of folders from a csv file and then run a python script inside each. The following code is to create the folders. It runs but doesn't create anything!
import errno
import os
import csv
import pandas as pd
mountains_column_name = "mountains"
#read from the csv of mountains
data = pd.read_csv('1500mountains.csv', encoding='utf8')
#column called mountains has the names:
if mountains_column_name not in data.columns:
raise ValueError("Missing Mountain column in input data")
mountains = data[mountains_column_name].tolist()
def mkdir_p(mountains):
try:
os.makedirs('\vince\Desktop\WhatMountainThat\dataset')
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and
os.path.isdir('\vince\Desktop\WhatMountainThat\dataset'):
pass
else:
raise
mkdir_p(mountains)
I'd forgotten to call mkdir! When I call it now I get the following :
TypeError: mkdir_p() missing 1 required positional argument: 'mountains'
Upvotes: 0
Views: 2439
Reputation: 1740
Lets assume that your_list_from_csv
vaiable is list of dirs you want to create. Then create floders like so:
import os
path = "/your/path"
your_list_from_csv = ["dir1", "dir2"]
for new_dir in your_list_from_csv:
dir_path = "{0:s}/{1:s}".format(path, new_dir)
try:
os.mkdir(dir_path)
except FileExistsError as err:
print("Folder Exists")
output:
$ ls -la
total 16
drwxrwxr-x 4 rszamszur rszamszur 4096 Nov 1 13:06 .
drwxrwxr-x 7 rszamszur rszamszur 4096 Nov 1 13:06 ..
drwxrwxr-x 2 rszamszur rszamszur 4096 Nov 1 13:06 dir1
drwxrwxr-x 2 rszamszur rszamszur 4096 Nov 1 13:06 dir2
For Windows users out there you will need to provide path with disk partition while using backslashes instead. Example:
path = "c:\your\path"
Upvotes: 1