0

I want to move files with similar names form one directory to another using os module in python 3.7.0. For example i have text files with names A1B1,A1B2,A2B1,A2B2 and so on. How to move files with name A1Bn (n=1,2,3...) in directory named A1 and files with name A2Bn (n=1,2,3...) in directory named A2. How to check the files name. Thanks ...

Abbas Ali
  • 165
  • 3
  • 9
  • 1
    use `glob` module in python [For Reference](https://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python) – Albin Paul Nov 08 '18 at 09:59

4 Answers4

0

Using glob and os (shutil can also be used):

import glob
import os

a1_files = glob.glob('A1*')
a2_files = glob.glob('A2*')

for filename in a1_files:
    os.rename(filename, os.path.join('A1', filename))

for filename in a2_files:
    os.rename(filename, os.path.join('A2', filename))
iz_
  • 15,923
  • 3
  • 25
  • 40
0
import os

base_path = "/path/to/files/"
filenames = os.listdir(base_path)

for f in filenames:
    source = base_path + f
    destination = base_path + f[:2] + "/" + f
    os.rename(source, destination)
bak2trak
  • 1,062
  • 8
  • 11
0

Use os.listdir to get all the file from the folder and os.rename to move the files.

import os


def file_reader():
    # get files
    for files in os.listdir("/path/to/your/files"):
        if files.endswith(".txt"):
            try:
                os.mkdir("A1")
                os.mkdir("A2")
            except FileExistsError:
                pass
            if files.startswith("A1"):
                os.rename(files, os.path.join("A1", files))
            if files.startswith("A2"):
                os.rename(files, os.path.join("A2", files))


if __name__ == "__main__":
    file_reader()
  • In this code you are creating directories for just to files named with "A1" and "A2". But i have 1000+ files with named "A1", "A2","A3", .... so on. How to create directories for all? – Abbas Ali Nov 09 '18 at 05:14
0

Here's a tweaked script that I used a while ago to achieve something similar:

from os import getcwd
from os import listdir
from os import makedirs

from os.path import join
from os.path import exists
from os.path import abspath

from shutil import move

current_path = getcwd()

for file in listdir("."):
    if file.startswith("A"):
        full_path = abspath(file)
        folder_prefix = file[:2]

        folder_path = join(current_path, folder_prefix)
        if not exists(folder_path):
            makedirs(folder_path)

        move(full_path, folder_path)

It copies all the files from your current directory that begin with A, and move them into their respective folders. It also makes the folders beforehand if they don't exist. You can tweak this to your liking by including your own paths, but it shows the general idea.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • In this code you are making directories for just to files named with "A1" and "A2" in dictionary(folder_paths) . But i have 1000+ files with named "A1", "A2","A3", .... so on. How to create directories for all? – Abbas Ali Nov 09 '18 at 05:18