0

I want to apply a test to list of files. The files that past the test should be moved to the directory "Pass"; the others should be moved to the directory "Fail".

Thus the output directory should contain subdirectories "Pass" and "Fail".

Here is my attempt:

        if(<scan==pass>) # Working fine up to this point
            dest_dir = outDir + '\\' + 'Pass'  # The problem is here
            print("Pass", xmlfile)
            MoveFileToDirectory(inDir, xmlfile, dest_dir)
        else:
            dest_dir = os.path.dirname(outDir + '\\' + 'Fail')
            print("Fail: ", xmlfile)
            MoveFileToDirectory(inDir, xmlfile, dest_dir)

However, my code is moving the files to the output directory and not creating the "Pass" or "Fail" subdirectories. Any ideas why?

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
HK0909
  • 19
  • 8
  • `if()` is not valid syntax at all, what is supposed to be testing? – Padraic Cunningham Aug 05 '15 at 10:53
  • are you sure about your syntax? forgetting about intentation, what does `<...>` do? and `pass` is a keyword. you should never use it as a variable. i think you can't even... – hiro protagonist Aug 05 '15 at 10:53
  • this is something i used according to my program,for the example i typed like this. – HK0909 Aug 05 '15 at 10:54
  • Yes I am sure pass is a keyword in lower case, to resolve ambiguity you can use Passed. my code is fine with identation while posting it appeared like this@hiro – HK0909 Aug 05 '15 at 10:56
  • `if()` makes absolutely no sense and is not fine, the problem is that line – Padraic Cunningham Aug 05 '15 at 11:01
  • It also misses a colon after the if, and it's unusual to wrap conditions in parentheses. A simple `if thecondition:` suffices. – deets Aug 05 '15 at 11:03
  • HK0909: I get what you did with , and simplification is good, but use something that is at least syntactically correct; e.g. `if( conditionTrue ):` –  Aug 05 '15 at 11:08
  • @JohnPirie, what is `if()` doing exactly? – Padraic Cunningham Aug 05 '15 at 11:12
  • 1
    @PadraicCunningham: I think OP just had some big wacky condition there that he is trying to spare us from looking at. It's just a true/false condition of some kind, not germane to the real problem. –  Aug 05 '15 at 11:15

2 Answers2

0

Use os.path.join(). Example:

os.path.join(outDir, 'Pass')

See this SO post

Also, we don't know what MoveFileToDirectory does. Use the standard os.rename:

os.rename("path/to/current/file.foo", "path/to/new/desination/for/file.foo")

See this SO post

So:

source_file = os.path.join(inDir, xmlfile)
if(conditionTrue):
    dest_file = os.path.join(outDir, 'Pass', xmlfile)
    print("Pass: ", xmlfile)
else:
    dest_file = os.path.join(outDir, 'File', xmlfile)
    print("Fail: ", xmlfile)
os.rename(source_file, dest_file)
Community
  • 1
  • 1
0

Create directories exactly once:

import os

labels = 'Fail', 'Pass'
dirs = [os.path.join(out_dir, label) for label in labels]
for d in dirs:
    try:
        os.makedirs(d)
    except EnvironmentError:
        pass # ignore errors

Then you could move files into the created directories:

import shutil

print("%s: %s" % (labels[condition_true], xmlfile))
shutil.move(os.path.join(out_dir, xmlfile), dirs[condition_true])

The code exploits that False == 0 and True == 1 in Python.

jfs
  • 399,953
  • 195
  • 994
  • 1,670