-4

I’m trying run this code on my Ubuntu machine so it will take any new .txt file from the out folder to the in folder, but I'm probably doing something wrong because it does not work.

import shutil
import os

os.chdir("/test/out")
srcdir = os.listdir("/test/out")
dstdir = "/test/in"
srcdir = True

def filemv():
    for file in srcdir:
        if file.endswith(".txt"):
            shutil.move(file, dstdir)
            print(file)
while srcdir is True:
    filemv()
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • This will max the CPU and probably cause the system to freeze. You should re-think your idea. – Joshua Strot Dec 24 '13 at 10:27
  • 1
    `for file in srcdir` fails, because it is True and you can't loop over that. – RemcoGerlich Dec 24 '13 at 10:27
  • Apart from the errors in your code already pointed out: What's your definition of "working"? What's the expected result, and what result do you actually get? – Lukas Graf Dec 24 '13 at 10:28
  • Also, you have declare `srcdir` two times... It's a boolean and not the result of `os.listdir()` as you might expected. – Maxime Lorant Dec 24 '13 at 10:34
  • hi Lukas, i want this little script to listen on the srcdir for new *.txt file. once new file got into the the folder it will immediately move to dstdir. thanks guy – user3132125 Dec 24 '13 at 10:39

1 Answers1

1

The issue is that you call os.listdir() just once. If you don't call it again, you won't notice any new files.

Also, running a busy loop for this sort of thing is probably a bad idea. Take a look at How to watch a directory for changes? and links therein.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012