| | |
how to move files to another Directory in Python?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 3
Reputation:
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:
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 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)
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 ?
Last edited by ZielonySBS; Feb 17th, 2009 at 6:36 pm.
I will challenge myself to try that, but check this!
http://timgolden.me.uk/python/win32_...py-a-file.html
http://www.ehow.com/how_4618897_copy...le-python.html
http://timgolden.me.uk/python/win32_...py-a-file.html
http://www.ehow.com/how_4618897_copy...le-python.html
Last edited by evstevemd; Feb 17th, 2009 at 7:49 pm.
It was as easy as this 
Don't worry, this is what python is

Don't worry, this is what python is
python Syntax (Toggle Plain Text)
import shutil src = "C:\\steve_test\\Test_xp\\added" dst = "C:\\steve_test\\Test_xp\\moved" shutil.move(src, dst)
•
•
Join Date: Feb 2009
Posts: 3
Reputation:
Solved Threads: 0
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 ?
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.
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!
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!
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
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:
Python Syntax (Toggle Plain Text)
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 ?
[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.
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
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]
Last edited by sisayie; Feb 20th, 2009 at 12:04 pm.
•
•
•
•
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)
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 ?
No one died when Clinton lied.
![]() |
Similar Threads
- Error Message (Python)
Other Threads in the Python Forum
- Previous Thread: Error while opening a file
- Next Thread: Validate user input
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






