[Tutor] simply moving files
Kent Johnson
kent37 at tds.net
Tue May 12 22:15:14 CEST 2009
On Tue, May 12, 2009 at 1:51 PM, Matt Herzog <msh at blisses.org> wrote:
> On monday I posted the below code:
>
> def schmove(src,dst):
> ... src = '/home/datasvcs/PIG/cjomeda_exp/'
> ... dst = '/home/datasvcs/PIG/cjomeda_exp_archive/'
> ... listOfFiles = os.listdir(src)
> ... for filez in listOfFiles:
> ... os.system("mv"+ " " + src + " " + dst)
>
> David Angel replied to my post and I guess I accidentally deleted his response. Today I found his response posted on mail-archive.com and tried some variations. They all failed.
>
> David said, "Just use os.rename() if the dest is a directory, it'll move the file there."
>
> Example: If I use: "os.rename(src, dst)" where I had "os.system("mv"+ " " + src + " " + dst)" no files are coppied.
>
> os.renames happily renames the source directory, but that's not what I want.
You are still not using the actual file name in your command. src is a
directory, so os.rename(src, dst) renames the directory. Try
os.rename(os.path.join(src, filez), os.path.join(dst, filez))
Kent
More information about the Tutor
mailing list