Python script create folders from filename

python create directory
python create file in directory
how to create folders and subfolders in python
python save file to new directory
python create folders from list
python copy file and rename
python script to copy files from one folder to another
python move file

Searching a python script thats move files to directions created based on the filenames.

Ex. Filename is ARC20180810185310.jpg Move that file to: 2018>08

Filename is ARC20180910185310.jpg Move that file to: 2018>09

It's good to try something yourself first, the ask on SO - you learn much more that way.

I would try:

import os
from shutil import copyfile
folder_path = '*folder path*'
for file in os.listdir(folder_path)
    year = file[3:7]
    month = file[7:9]
    final_path = folder_path + '/' + year + '>' + month + '/' + file + .jpg'
    copyfile(file, final_path)

Just replace the paths in ** with whatever you need.

This way you slice the file name and get year and month from it (characters from 3 to 6 and 7,8), then copyfile to a path consisting of both year and month that you sliced.

Using Python to create directories and move files, Using Python to create directories and move files SMDR, the collection code for the items digitized; 1959, the year the negatives are from The folder therefore only needs sections 1-3 of the filename, i.e. SMDR_1959-185. Python script create folders from filename. Searching a python script thats move files to directions created based on the filenames. Ex. Filename is ARC20180810185310.jpg Move that file to: 2018>08. ARC20180810185310.jpg. 2018>08. Filename is ARC20180910185310.jpg Move that file to: 2018>09. ARC20180910185310.jpg. 2018>09

Just replace the srcDir and dstDir, you will get your file.jpg moved, if you use MoveFile(srcDir, dstDir, resuisive=True), you will move the file.jpg in the sub-dir of the srcDir too.

from __future__ import print_function
import os, re, shutil

class MoveFile(object):
    def __init__(self, srcDir, dstDir, recursive=False, flag='.JPG'):
        self.srcDir = srcDir
        self.dstDir = dstDir
        self.recursive = recursive
        self.flag = flag
        self.duplicateFileName = []
        self.badFileName = []
        self.jpgFile = []
        self.srcDirDict = {}

    def findAllJPG(self):
        # recursively find file 
        if self.recursive == False:        
            for item in os.listdir(self.srcDir):
                if os.path.isfile(os.path.join(self.srcDir,item)) and \
                                os.path.splitext(item)[-1] == self.flag.lower():
            for root, dirs, files in os.walk(self.srcDir):
                for item in files:
                    if os.path.splitext(item)[-1] == self.flag.lower():
                        self.srcDirDict[item] = root

        if not self.jpgFile: 
            print('NOT FIND ANY JPG FILE!')
        return self.jpgFile

    def parse(self, text):
            pat =re.compile('[a-zA-Z]+([\d]+)')
            match = pat.match(text)
            data =
            fileName = data[:4]+'-'+data[4:6]
        except TypeError:
            fileName = None
        return fileName  

    def move(self, text):
            fileName = self.parse(text)
            if fileName == None: return
            if not os.path.isdir(os.path.join(self.dstDir, fileName)):

            srcPath= os.path.join(self.srcDirDict[text], text)
            dstDir = os.path.join(self.dstDir, fileName)
            shutil.move(srcPath, dstDir)

    def decC(dir):
        return os.path.join(self.srcDir,dir)

    def run(self):
            if not os.path.isdir(self.dstDir):
            for text in self.findAllJPG():
            print('MOVE SUCCESSFUL!') 

srcDir = r'C:\Users\Administrator\Desktop\2'
dstDir = r'C:\Users\Administrator\Desktop\3'
fmv = MoveFile(srcDir, dstDir, recursive = False)

Creating and Deleting Directories with Python, Listing 1 shows how to integrate this method in a Python script. # import the os module import os # detect the current working directory and print it path  how do you create a python script that collects file name, size, and date created I am trying to create a simple python application that takes two parameters and collects all the file names, size, and date created in a given folder.

First of all you will need glob.glob() function to find all files in given directory:

files = glob.glob('ARC*.jpg')

Then you will need to extract some parts of filename:

year = filename[3:7]
month = filename[7:9]

Use os.makedirs() with exist_ok=True and create the dirs:

os.makedirs(os.path.join(BASE_DIR, year, month))

Then use shutil.move to move file to specific directory

shutil.move(filename, os.path.join(BASE_DIR, year, month, filename))

Finally you will get something like this:

import glob
import os.path
import shutil

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

files = glob.glob('ARC*.jpg')
for filename in files:
    year = filename[3:7]
    month = filename[7:9]
        os.makedirs(os.path.join(BASE_DIR, year, month))
    except OSError:
    shutil.move(filename, os.path.join(BASE_DIR, year, month, filename))

Chapter 9 – Organizing Files, Removing the leading zeros in the filenames for every file in a folder of hundreds This code will create a new ZIP file named that has the compressed  Batch create folders based on part of file name and move files into that folder; Batch Created Folder By Filename Part and Move; Batch create folders based on series of files and move to subdirectory; Batch script to create folders based on filenames; create folders based on a file name and move those files into that folder

Generating Random Quiz Files, Any filenames or paths that do not begin with the root folder are assumed to Your programs can create new folders (directories) with the os.makedirs() function. Text files with the .txt extension or Python script files with the .py extension are  I think I need two loops to create each folder and then create the list of subfolders within each folder. – jredd Mar 11 '16 at 22:27 | show 1 more comment 2

Working With Files in Python – Real Python, The code above can be made more concise if you combine the for loop and the if To create a single directory, pass a path to the directory as a parameter to  Python File Handling: Create, Open, Append, Read, Write. In Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing and reading files.

Analyzing Your File System and Folder Structures with Python , filepaths = [os.path.join(folder, f) for f in os.listdir(folder)] to see the links between files and folders which can be used to create a graph. In this case, we would expect mostly Python script files and possibly some other files  To get a list of all the files and folders in a particular directory in the filesystem, use os.listdir () in legacy versions of Python or os.scandir () in Python 3.x. os.scandir () is the preferred method to use if you also want to get file and directory properties such as file size and modification date.

  • What have you tried so far?
  • have a look at year and month in the beginning of your post, seems like there's a typo and you take wrong values, like 0180 instead of 2018. In the final version it's fine though :)
  • Thanks but goy a problem that in Python "exist_ok=True" not working.