在python中从csv创建文件夹

时间:2018-11-01 11:51:50

标签: python csv

我正在尝试从一个csv文件创建多个文件夹,然后在每个文件夹中运行一个python脚本。以下代码用于创建文件夹。它可以运行,但不会创建任何东西!

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)

我忘了给mkdir打电话!现在调用它时,我得到以下信息:

TypeError:mkdir_p()缺少1个必需的位置参数:“ mountains”

1 个答案:

答案 0 :(得分:0)

让我们假设+-----+------+ |name | trend| +-----+------+ | a| xx| | b| yy| +-----+------+ 可用是您要创建的目录列表。然后像这样创建花絮:

your_list_from_csv

输出:

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")

编辑

对于在那里的Windows用户,您需要在使用反斜杠的同时为磁盘分区提供路径。示例:

$ 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