how to move files to another Directory in Python?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 3
Reputation: ZielonySBS is an unknown quantity at this point 
Solved Threads: 0
ZielonySBS ZielonySBS is offline Offline
Newbie Poster

how to move files to another Directory in Python?

 
0
  #1
Feb 17th, 2009
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:

  1. import shutil
  2.  
  3. src = "/home/scanner/."
  4. dst = "/home/Administrator/"
  5.  
  6. 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 ?
Last edited by ZielonySBS; Feb 17th, 2009 at 6:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,448
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is online now Online
Nearly a Posting Virtuoso

Re: how to move files to another Directory in Python?

 
0
  #2
Feb 17th, 2009
Last edited by evstevemd; Feb 17th, 2009 at 7:49 pm.
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,448
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is online now Online
Nearly a Posting Virtuoso

Re: how to move files to another Directory in Python?

 
1
  #3
Feb 17th, 2009
It was as easy as this
Don't worry, this is what python is
  1. import shutil
  2. src = "C:\\steve_test\\Test_xp\\added"
  3. dst = "C:\\steve_test\\Test_xp\\moved"
  4. shutil.move(src, dst)
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3
Reputation: ZielonySBS is an unknown quantity at this point 
Solved Threads: 0
ZielonySBS ZielonySBS is offline Offline
Newbie Poster

Re: how to move files to another Directory in Python?

 
0
  #4
Feb 17th, 2009
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 ?
Last edited by ZielonySBS; Feb 17th, 2009 at 9:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,448
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is online now Online
Nearly a Posting Virtuoso

Re: how to move files to another Directory in Python?

 
0
  #5
Feb 17th, 2009
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!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
---- Python, C++ PHP and Java ----
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3
Reputation: ZielonySBS is an unknown quantity at this point 
Solved Threads: 0
ZielonySBS ZielonySBS is offline Offline
Newbie Poster

Re: how to move files to another Directory in Python?

 
0
  #6
Feb 18th, 2009
evstevemd please write me that piece of code.

I have been strugled with it for two days.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 945
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: how to move files to another Directory in Python?

 
0
  #7
Feb 18th, 2009
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.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: sisayie is an unknown quantity at this point 
Solved Threads: 0
sisayie sisayie is offline Offline
Newbie Poster

Re: how to move files to another Directory in Python?

 
0
  #8
Feb 20th, 2009
Originally Posted by ZielonySBS View Post
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:

  1. import shutil
  2.  
  3. src = "/home/scanner/."
  4. dst = "/home/Administrator/"
  5.  
  6. 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]
Last edited by sisayie; Feb 20th, 2009 at 7:26 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: sisayie is an unknown quantity at this point 
Solved Threads: 0
sisayie sisayie is offline Offline
Newbie Poster

Re: how to move files to another Directory in Python?

 
0
  #9
Feb 20th, 2009
Originally Posted by sisayie View Post
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]
Last edited by sisayie; Feb 20th, 2009 at 12:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: how to move files to another Directory in Python?

 
0
  #10
Feb 20th, 2009
Originally Posted by ZielonySBS View Post
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:

  1. import shutil
  2.  
  3. src = "/home/scanner/."
  4. dst = "/home/Administrator/"
  5.  
  6. 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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC