0

I have a folder which contains around 500 files.

I have a problem where I am creating folders based on those 500 file names. For e.g., if I have files such as A.txt, B.txt, etc first I want to create folders named 'A' and 'B' and then push 'A.txt' file into a folder named 'A' and 'B.txt' named file into folder named 'B', which I have just created. SO basically two tasks first is to create the folders based on the file names and then push the corresponding files into the named folders.

However, I'm getting stuck in two places, first, the folders name are getting created as 'A.txt', 'B.txt' etc instead on 'A' or 'B' as I am taking the filename itself and second is to place the files into corresponding folders.

I have tried below code:

       import os, shutil, glob
       import pandas as pd

       def i2f(directory):
       for filename in os.listdir(directory):
           foldername = filename
           folder_loc = "all_files\user\txt-images"
           crfolder(os.path.join(folder_loc, foldername))
           '''
           crfolder is function that creates a folder
           '''
           src_dir = r"all_files\user\txt-images\src_folder" 
           dstn_dir = r"all_files\user\txt-images\trgt_folder" 
           for file in glob.glob("\\*.txt"):
           re.compile(r"[^_.A-Z]")
           shutil.copy2(file, dstn_dir)

       def crfolder():
           import os
           try:
              if not os.path.exists(folder_loc):
                 os.makedirs(folder_loc)
           except OSError:
              print ('''Can't create directory! ''' +  folder_loc)

Any help is appreciated to tell me where I am getting it wrong.

user11409134
  • 79
  • 1
  • 11
  • Look at this : https://stackoverflow.com/questions/41826868/moving-all-files-from-one-directory-to-another-using-python. This doesn't tell about creating directories but you can do this with `os.mkdir` – Ashargin May 14 '19 at 17:16

2 Answers2

1

to make things simple:

import os, shutil
parent_folder = 'myfolder'

# get files only not folders
files = [name for name in os.listdir(parent_folder) if os.path.isfile(os.path.join(parent_folder, name))]

for f_name in files:
    file = os.path.join(parent_folder, f_name)  # full path

    folder_name = f_name.split('.')[0]  # remove file extension
    folder = os.path.join(parent_folder, folder_name)  # full path

    if not os.path.exists(folder):  # make folder if not existed before
        os.mkdir(folder)

    shutil.move(file, os.path.join(folder, f_name))  # move file
Mahmoud Elshahat
  • 1,873
  • 10
  • 24
  • your code won't work correctly if the filename contains more then one period. ex. `hello.world.txt` or `elshhat.goog.png`, would create folders `hello` and `elshhat` in your code, which isn't correct. Instead use `os.path.splitext(x)[0] ` as it will give you the actual filename, without the extension. – Vasu Deo.S May 14 '19 at 19:40
0

Try this:

import os
import shutil

path = r"C:\Users\vasudeos\OneDrive\Desktop\newfolder"

for x in os.listdir(path):

    file = os.path.join(path, x)

    if not os.path.isdir(file):
        folder_name = os.path.join(path, os.path.splitext(x)[0])

        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        shutil.move(file, folder_name)
Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23