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
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
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 ?
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()
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
2,781 posts since Jul 2008
Reputation Points: 1,024
Solved Threads: 691