4

I am trying to write a script (python 2.7) that will use a regex to identify specific files in a folder and move them to another folder. When I run the script, however, the source folder is moved to the target folder instead of just the files within it.

import os, shutil, re

src = "C:\\Users\\****\\Desktop\\test1\\"
#src = os.path.join('C:',  os.sep, 'Users','****','Desktop','test1\\')
dst = "C:\\Users\\****\\Desktop\\test2\\"
#dst = os.path.join('C:',  os.sep, 'Users','****','Desktop','test2')

files = os.listdir(src)
#regexCtask = "CTASK"
print files
#regex =re.compile(r'(?<=CTASK:)')


files.sort()

#print src, dst

regex = re.compile('CTASK*')

for f in files:
    if regex.match(f):
        filescr= os.path.join(src, files)
        shutil.move(filesrc,dst) 
        #shutil.move(src,dst) 

So basically there are files in "test1" folder that I want to move to "test2", but not all the files, just the ones that contain "CTASK" at the beginning.

The **** in the path is to protect my work username.

Sorry if it is messy, I am still trying a few things out.

1
  • 1
    os.path.join(src, files) should be generating an exception. (Assuming any files in the source start with "CTAS" plus 0 or more "K" characters, which is probably not what you actually meant it to be looking for.) Commented Jan 26, 2017 at 15:11

2 Answers 2

11

You need to assign path to exact file (f) to filescr variable on each loop iteration, but not path to files (files - is a list!)

Try below code

import os
from os import path
import shutil

src = "C:\\Users\\****\\Desktop\\test1\\"
dst = "C:\\Users\\****\\Desktop\\test2\\"

files = [i for i in os.listdir(src) if i.startswith("CTASK") and path.isfile(path.join(src, i))]
for f in files:
    shutil.copy(path.join(src, f), dst)
Sign up to request clarification or add additional context in comments.

6 Comments

Great, that worked! Could you please explain exactly what the path module does in this instance?
path.join() used to path_to_folder + file_name = path_to_file. path.isfile()- just to check that item in listdir(src) is a file, but not a folder- you might remove this condition if needed
One more quick question. How would I apply this to folders instead of just files? edit: neverimind. path.isdir() is what I needed.
Try just to use i for i in os.listdir(src) if not path.isfile(path.join(src, i)) or i for i in os.listdir(src) if path.isdir(path.join(src, i)). This should search for folders only. If you need files as well as folders, just remove this kind of conditions
You can simply use if "string" in file_name: ...
|
0

I wanted to move following folders : 1.1,1.2,1.45,1.7 to folder with name '1' I Have posted solution below:

import shutil
import os

src_path = '/home/user/Documents/folder1'
dest_path='/home/user/Documents/folder2/'
source = os.listdir(src_path)

for folder in source :
    #folder = '1.1 -anything'
    newf = folder.split('.')[0] 
#newf is name of new folder where you want to move 
#change Folder name as per yourrequirement

    destination = dest_path+newf
    if not os.path.exists(destination):
        os.makedirs(destination)
    shutil.move(src_path+'/'+folder,destination) #change move to copy if you want to copy insted of moving 
    print 'done moving'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.