-1

Is there a Python method to create directories recursively? I have this path:

/home/data/ with files 'table1.csv', 'table2.csv', ... , 'table1000.csv' in it

I would like to create: /home/data/table1 and move 'table1.csv' in it; /home/data/table2 and move 'table2.csv' in it; . . . /home/data/table1000 and move 'table1000.csv' in it;

The folder names would have to match the csv file names.

How can I do it recursively? I know os.makedirs() should probably be used not sure how it works exactly.

Many thanks.

NOTE: 'table1' and 'table2' etc are just dummy example file names. The real filenames are a bit complex.

Katherine
  • 39
  • 6
  • I don't think creating folders recursively is exactly what you're looking for as the folders that you need to create all sit at the same level. You just need a loop and use ```os.mkdir(path)``` at each iteration – kerwei Jan 31 '19 at 09:10

2 Answers2

3

I would work the following way in Python:

1.Get all files in folder in a list

2.Loop Through the filenames of the list and:

  1. Create Folder with the exact name (provided that you do not have duplicates)
  2. Move file in the folder
  3. Next File

A simple search in the net would get you ready examples on how to do each step of the above.

Edit: Below a simple example for csv files.

import glob, os
import shutil
dir="D:\Dropbox\MYDOCS\DEV\python\snippets"
os.chdir(dir)
for file in glob.glob("*.csv"):
    dst=dir+"\\"+file.replace(" ","_").replace(".csv","")
    os.mkdir(dst)
    print(dst)
    shutil.move(file,dst)

Used windows paths, since I use windows, you ll need to change that to linux paths.

Katherine
  • 39
  • 6
TharsDimitris
  • 154
  • 10
1

Use mkdir to create each dir, from the os library.

https://docs.python.org/2/library/os.html

For each dir move the current file using shutil.move.

How to move a file in Python

Each iteration should look like this:

for i in range(1, 1001):
    os.mkdir('/home/data/table' + str(i))
    shutil.move('/home/data/table' + str(i) + '.csv', '/home/data/table' + str(i) + '/table' + str(i) + '.csv')
Nexaspx
  • 371
  • 4
  • 20
  • how does mkdir work in terms of making sure the folder name match my existing file name? – Katherine Jan 31 '19 at 09:31
  • @Katherine Not quite sure I understand you. `mkdir` creates a directory with a path you give it. So make sure you give it the right name. Please read the documentation and also note that errors may be thrown. – Nexaspx Jan 31 '19 at 09:34
  • You get the name of the file without the extension via the `os.path.splitext` function. Then you call mkdir with that. And the I suggest using `os.rename`. – Dan D. Jan 31 '19 at 09:35
  • @Katherine If you know the names of the files and wanted directories, why bother with splitting and extensions? Create each dir and move the file with the same number to it into it. – Nexaspx Jan 31 '19 at 10:16
  • The question was just a simple dummy example. The real file names aren't that simple and I need to do it for 2TB worth of data files. @Nexaspx – Katherine Jan 31 '19 at 10:18
  • @Katherine Perhaps you should edit your original question to better reflect your needs. – Nexaspx Jan 31 '19 at 10:39