Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
automatic create folders
#1
Hello,

still new here so I hope i came to the right place.

I would like to automatically create empty folders from a csv file

But I ran into a Wall my code only generates a folder for the last row in the csv file....

any help is much appreciated

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 30 18:56:27 2021

@author: [email protected]

code automatisch mapstructuur aan te maken op basis van een .csv bestand

incomplete -- incompleet -- incomplete --

creeert alleen de laatste folder

"""

# -*- coding: utf-8 -*-

"""

Spyder Editor



This is a temporary script file.

"""



import os

import csv

#path= 'X:\\Medewerkers\\mkool\\00 ALGEMEEN'
path= 'D:\\Documents\\MARCEL\BIM, CAD & GIS\\PY_SCRIPTS'
f= open('D:\\Documents\\MARCEL\BIM, CAD & GIS\\PY_SCRIPTS\\name.csv')
#f= open('X:\\Medewerkers\\mkool\\00 ALGEMEEN\\map1.csv')

csv_f = csv.reader(f)

 
#lets see what we have

for row in csv_f:

    print (row)

   

os.chdir(path)

 

#os.chdir(path)

#for i in range(1,11):

#row= (Newfolders)

Newfolders= str(row)

os.makedirs(Newfolders)
Larz60+ write Dec-20-2021, 09:56 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode togs on future posts.
Reply
#2
Pathlib is a better way to go:
the following code will create a directory tree directly below the script that runs it
It is non-destructive and will only create the directory if it doesn't already exist:
Output:
── data │   ├── csv │   ├── html │   ├── json │   └── tmp
code:
import os
from pathlib import Path

class MyPaths:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))

        homepath = Path(".")

        self.datapath = homepath / "data"
        self.datapath.mkdir(exist_ok=True)

        self.csvpath = self.datapath / "csv"
        self.csvpath.mkdir(exist_ok=True)

        self.jsonpath = self.datapath / "json"
        self.jsonpath.mkdir(exist_ok=True)

        self.htmlpath = self.datapath / "html"
        self.htmlpath.mkdir(exist_ok=True)

        self.tmppath = self.datapath / "tmp"
        self.tmppath.mkdir(exist_ok=True)


if __name__ == "__main__":
    MyPaths()
Reply
#3
Assuming that your csv file looks something like this:
Output:
first,second,third,fourth
Then this code should do the trick:
import os

path= 'D:\\Documents\\MARCEL\BIM, CAD & GIS\\PY_SCRIPTS'
os.chdir (path)

with open ('map1.csv', 'r') as names_file :
	directory_list = names_file.read ().strip ().split (',')

for directory_name in directory_list :
	os.makedirs(directory_name)
Reply
#4
Hey guys

thanks a lot already


You asked me
Quote:what error do you get ?
]

Well None...! I did not get any. Although the output was less then what I desired.

I have attached a dummy-file so you have a better understanding of the data.
The desired outcome is folder names (combined street and numbers) e.g.; Sesamestreet 5, Sesamestreet 6, Gravelpit 1, etc.

M

Attached Files

.csv   name.csv (Size: 379 bytes / Downloads: 84)
Reply
#5
(Dec-21-2021, 04:21 PM)Mr_Kool Wrote: I have attached a dummy-file so you have a better understanding of the data.
The desired outcome is folder names (combined street and numbers) e.g.; Sesamestreet 5, Sesamestreet 6, Gravelpit 1, etc.
This script should give you the results that your are looking for with the file sample that you provided.
import os
 
path= 'D:\\Documents\\MARCEL\BIM, CAD & GIS\\PY_SCRIPTS'
os.chdir (path)
 
with open ('name.csv', 'r') as names_file :
	names_file.readline ()
	for line in names_file :
		line_list = line.split (',')
		directory_name = line_list [0] + line_list [1]
		os.makedirs (directory_name)
Mr_Kool likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 357 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,481 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Matplotlib - automatic update frohr 1 1,110 Mar-29-2022, 07:05 PM
Last Post: deanhystad
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,793 May-18-2021, 06:42 PM
Last Post: Skaperen
  sub-folders in folders from text line jenost 1 1,579 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Automatic registering python to registry kozaizsvemira 1 2,201 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  Automatic redefining Hassediagram 5 3,302 Feb-25-2019, 01:36 PM
Last Post: Hassediagram
  create folders named from list array HaKDMoDz 1 4,959 Jul-19-2018, 06:09 AM
Last Post: buran
  Idea of timelapse with automatic settings hhanswurster 2 3,254 May-26-2018, 01:43 PM
Last Post: killerrex
  python to install printer drivers automatic leoxucn 4 7,688 Jul-10-2017, 09:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020