Here is a simple Python script that I wrote with lots of help from the Python mailing list. I moves all the files of subdirectories to the top level directory. It works well for my use, but if there are things that can be improved then I would like to know. I ask in the interest of being a better Python programmer and learning the language better. Thanks.#!/usr/bin/python# -*- coding: utf-8 -*-import sysimport osimport shutilcurrentDir = os.getcwd()forReal = Falseif ( currentDir in ['/home/dotancohen', '/home/dotancohen/.bin'] ):        print Error: Will not run in +currentDir        exit()if ( len(sys.argv)>1 and sys.argv[1] in ['m', 'M', 'c', 'C'] ):        forReal = Trueelse:        print \nThis is a test run. To actually perform changes, add 'm' (move) or 'c' (copy) after the program name.\nfilesList = os.walk(currentDir)for rootDir, folders, files in filesList:        for f in files:                if (rootDir!=currentDir):                        toMove = os.path.join(rootDir, f)                        print toMove                        newFilename = os.path.join(currentDir,f)                        renameNumber = 1                        while(os.path.exists(newFilename)):                                newFilename = os.path.join(currentDir,f)+_+str(renameNumber)                                renameNumber = renameNumber+1                        if ( forReal and sys.argv[1] in ['m', 'M'] ):                                os.rename(toMove, newFilename)                        elif ( forReal and sys.argv[1] in ['c', 'C'] ):                                shutil.copy(toMove, newFilename)if ( not forReal ):        print \nThis was a test run. To actually perform changes, add 'm' (move) or 'c' (copy) after the program name.
Python: flatten directories
python
Peter has already mentioned putting the code into methods. This isnt just a personal preference, its a general recommendation that all Python code should follow.In fact, consider the following an idiomatic Python code outline:import sysdef main():    # if __name__ == '__main__':    sys.exit(main())Implement PEP 8 Style Guide for Python Code. In particular, use underscore_separated variables instead of camelCase.Programs should follow the general flow (1) input, (2) processing, (3) output. Argument reading is part of (1); consequently, do it only once, at the start.Set variables depending on that input.perform_move = sys.argv[1] in ['M', 'm']perform_copy = sys.argv[1] in ['C', 'c']dry_run = not perform_copy and not perform_moveNote that Ive inverted the conditional for forReal. First of all, because its more common (dry run is what this is usually called). Secondly, the name forReal is rather cryptic.Put the ['/home/dotancohen', '/home/dotancohen/.bin'] into a configurable constant at the beginning of the code, or even into an external configuration file. Then make it a parameter to the method which performs the actual logic.By contrast, the filesList variable is unnecessary. Just  iterate directly over the result of os.walk.Operations of the form renameNumber = renameNumber+1 should generally be written as rename_number += 1. Also, pay attention to consistent whitespace usage.Id put the logic to find a unique target path name into its own method unique_pathname. Then Id replace the concatenation by string formatting.def unique_pathname(basename):    unique = basename    rename_no = 1    while os.path.exists(unique):        unique = '{0}_{1}'.format(basename, rename_no)    rename_no += 1    return uniqueNotice that Ive also removed the redundant parenthese around the loop condition here. The same should be done for all your if statements.if ( forReal and sys.argv[1] in ['m', 'M'] ):The test for forReal here is redundant anyway, its implied in the second condition. But weve got variables now anyway:if perform_move:    # elif perform_copy:    # 