Start New Discussion within our Software Development Community

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 ?

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)

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 ?

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 please write me that piece of code.

I have been strugled with it for two days.

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.

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

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)

I hope this will help

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)

This one is simpler and more readable

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)

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.

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)
#!/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()
#!/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).

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.

What about files specifc with specific names?

I think it is best to describe your issue in details and start your own thread.

This article has been dead for over six months. Start a new discussion instead.