943,587 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 27383
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 17th, 2009
0

how to move files to another Directory in Python?

Expand 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:

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZielonySBS is offline Offline
3 posts
since Feb 2009
Feb 17th, 2009
0

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

Last edited by evstevemd; Feb 17th, 2009 at 7:49 pm.
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Feb 17th, 2009
1

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

It was as easy as this
Don't worry, this is what python is
python Syntax (Toggle Plain Text)
  1. import shutil
  2. src = "C:\\steve_test\\Test_xp\\added"
  3. dst = "C:\\steve_test\\Test_xp\\moved"
  4. shutil.move(src, dst)
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Feb 17th, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZielonySBS is offline Offline
3 posts
since Feb 2009
Feb 17th, 2009
0

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

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!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Feb 18th, 2009
0

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

evstevemd please write me that piece of code.

I have been strugled with it for two days.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ZielonySBS is offline Offline
3 posts
since Feb 2009
Feb 18th, 2009
0

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

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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Feb 20th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by ZielonySBS ...
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:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sisayie is offline Offline
2 posts
since Feb 2009
Feb 20th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by sisayie ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sisayie is offline Offline
2 posts
since Feb 2009
Feb 20th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by ZielonySBS ...
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:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: install _imagingft
Next Thread in Python Forum Timeline: replace text in specific position of specific line





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC