954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to move files to another Directory in Python?

I have a company scanner but very often people forget to remove scanned (confidencial documents)

I want to create a script which will once day check a "/home/scanner" folder and if there are any files it moves them to my /home/Administrator folder.

I dont really dont know how to start. For now I create this:

import shutil

src = "/home/scanner/."
dst = "/home/Administrator/"

shutil.move(src, dst)


I know that os.walk() and listdir() shoud do a job but I donk know how to write a code.;-(

Would you be able to help me ?

ZielonySBS
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

It was as easy as this :)
Don't worry, this is what python is :D

import shutil
src = "C:\\steve_test\\Test_xp\\added"
dst = "C:\\steve_test\\Test_xp\\moved"
shutil.move(src, dst)
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

this code is exctly like mine. The problem with it is that it works only once well.

In next execution it has an error that the Directory has already exist

I want to run a script once a day and move files from src = "/home/scanner/

to

dst = "/home/Administrator/"

File names will be always different so there is not a problem with it.

Ideally it would be:

Script run:

1. Checks if there is any files in directory /home/scanner/
2. if there is nothing then do nothing
else:
move (content of /home/scanner) to /home/Administrator/

Next day the same

Any ideas how to do it ?

ZielonySBS
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Ok, I think you have to do Little trick here. Here is how I would go in such case:
1. I will take list of all files in scr directory
2. I will iterate in the list, moving each file to dst until I finish

I guess you know the right module and looping to do the job! :)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

evstevemd please write me that piece of code.

I have been strugled with it for two days.

ZielonySBS
Newbie Poster
3 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

have you tried os.walk. That is a function that will iterate throught the files and sub directories of the folder you are talking about.

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

I have a company scanner but very often people forget to remove scanned (confidencial documents)

I want to create a script which will once day check a "/home/scanner" folder and if there are any files it moves them to my /home/Administrator folder.

I dont really dont know how to start. For now I create this:

import shutil

src = "/home/scanner/."
dst = "/home/Administrator/"

shutil.move(src, dst)

I know that os.walk() and listdir() shoud do a job but I donk know how to write a code.;-(

Would you be able to help me ?

I hope this will help

[CODE = Python]
import os, subprocess
src = "/path/to/your/source/directory"
dst = "/path/to/your/destination/directory/"
listOfFiles = os.listdir(src)
for f in listOfFiles:
fullPath = src + "/" + f
subprocess.Popen("mv" + " " + fullPath + " " + dst,shell=True)[/CODE]

sisayie
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I hope this will help

[CODE = Python] import os, subprocess src = "/path/to/your/source/directory" dst = "/path/to/your/destination/directory/" listOfFiles = os.listdir(src) for f in listOfFiles: fullPath = src + "/" + f subprocess.Popen("mv" + " " + fullPath + " " + dst,shell=True) [/CODE]


This one is simpler and more readable

[CODE = Python]
import os
def move(src,dst):
src = "/path/to/your/source/directory"
dst = "/path/to/your/destination/directory/"
listOfFiles = os.listdir(src)
for f in listOfFiles:
fullPath = src + "/" + f
os.system ("mv"+ " " + src + " " + dst)[/CODE]

sisayie
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I have a company scanner but very often people forget to remove scanned (confidencial documents)

I want to create a script which will once day check a "/home/scanner" folder and if there are any files it moves them to my /home/Administrator folder.

I dont really dont know how to start. For now I create this:

import shutil

src = "/home/scanner/."
dst = "/home/Administrator/"

shutil.move(src, dst)

I know that os.walk() and listdir() shoud do a job but I donk know how to write a code.;-(

Would you be able to help me ?

When moving an entire directory with shutil.move will remove this directory, since it basically uses copy and then remove.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Use something like this that only moves files:

# move files from one directory to another
# if files alrady exist there, they will be overwritten
# retains original file date/time

import os
import shutil

# make sure that these directories exist
dir_src = "C:\\Python25\\ATest1\\"
dir_dst = "C:\\Python25\\ATest2\\" 

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)
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 
#!/usr/bin/python
import sys, os, time, shutil

print time.ctime()

path = "/home/richard/test/"
files = os.listdir(path)
files.sort()

for f in files:
	src = path+f
	dst = "/home/richard/Desktop/test/" +f
	shutil.move(src, dst)
print time.ctime()
longr59
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
#!/usr/bin/python
import sys, os, time, shutil

print time.ctime()

path = "/home/richard/test/"
files = os.listdir(path)
files.sort()

for f in files:
	src = path+f
	dst = "/home/richard/Desktop/test/" +f
	shutil.move(src, dst)
print time.ctime()


Using functions from this code snippet and this code snippet , you can also write

with WorkingDirectory("/home/richard"):
    tgz = tar_cz("test")
with WorkingDirectory("/home/richard/Desktop"):
    tar_xz(tgz)

This only works if directory "test" is not too large, because the whole content is loaded in memory (but compressed).

Gribouillis
Posting Maven
Moderator
2,781 posts since Jul 2008
Reputation Points: 1,024
Solved Threads: 691
 

There is another way. Maybe try using os.popen()?

You could

for i in listdir("path-to-dir"):
os.popen("move "+i+"other-dir")

Idk that's just my take on it.

Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 59
Solved Threads: 33
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You