0

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'

3
  • 1
    Have you called the function mkdir_p(mountains) ? Commented Nov 1, 2018 at 11:52
  • is it necessary to use pandas here? Commented Nov 1, 2018 at 12:03
  • Thanks @Rakesh I am an idiot! Commented Nov 1, 2018 at 12:08

1 Answer 1

1

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

Edit

For Windows users out there you will need to provide path with disk partition while using backslashes instead. Example:

path = "c:\your\path"
Sign up to request clarification or add additional context in comments.

13 Comments

This looks great. If I then want to run a script that will take a variable with the same name as the folder would I just nest that in this function?
I'm not sure what you mean, but if the directory already exists, FileExistsError is raised. So you will probably need simpre try except block. But basically this will create folders with names defined in your_list_from_csv variable under path which is defined in path variable
@VincentRyan you can either pass list of folders to crate. Or skip for loop and pass single item. BTW I've added try except block for recovering when folder already exists.
FileNotFoundError: [WinError 3] The system cannot find the path specified: '/vince/Desktop/WhatMountainThat/dataset/Mount Everest Himalayas'
@VincentRyan You are using windows which has different file structure than linux. Try like this: path = "c:/vince/Desktop/WhatMountainThat/dataset" (I guess this is probably on C drive)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.