0

Hey all,

I have the following code that is working fine; basically it finds all files on my desktop and then prints the specific ones that I am looking of.

I save all files to my desktop and the files contain the clients name so I figure once a week I can clean my desktop by sending the files off to where they live in the correct directories.

Here is what I have so far...

import glob
import os

os.chdir("d:\Desktop")
for files in glob.glob("*.*"):
    x = str(files).lower()
    if 'client_name' in x:
        print x

The next bit is to gt the fild to actually move, I found this in another thread but I'm having trouble getting them to play together!

import os
import shutil
# make sure that these directories exist
dir_src = "d:\Desktop"
dir_dst = "d:\Desktop\New folder" 
for file in os.listdir(dir_src):
    print file  # testing  
    src_file = os.path.join(dir_src, file)
    dst_file = os.path.join(dir_dst, file)
    shutil.move(src_file, dst_file)

I'd be greatful if anyone can help me out or direct me in the right direction.

Thanks :)

2
Contributors
3
Replies
68
Views
3 Years
Discussion Span
Last Post by Craig_2
Featured Replies
  • Very good. I would have written `shutil.move(files, client_folder)`. Another solution is to create a sequence of files to move first def to_move(folder): """generate the files to move""" wd = os.getcwd() os.chdir(folder) try: for name in glob.glob('*.*'): if 'client_name' in str(name).lower(): yield os.path.join(folder, name) finally: os.chdir(wd) src_dir = r'd:\Desktop' L = … Read More

1

Got there in the end if anyone is interested

import glob
import os
import shutil

os.chdir("d:\Desktop")
client_folder = "\clients\name"

for files in glob.glob("*.*"):
    x = str(files).lower()
    if 'client_name' in x:
        print x
        shutil.move(x, client_folder)
1

Very good. I would have written shutil.move(files, client_folder). Another solution is to create a sequence of files to move first

def to_move(folder):
    """generate the files to move"""
    wd = os.getcwd()
    os.chdir(folder)
    try:
        for name in glob.glob('*.*'):
            if 'client_name' in str(name).lower():
                yield os.path.join(folder, name)
    finally:
        os.chdir(wd)

src_dir = r'd:\Desktop'
L = list(to_move(src_dir))
0

Thanks for this, I'll have to dissect your code as I'm not fully sure whats going on by just looking at it - Still a noob :)

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.