Python windows folders

Hi im very new to python, im try to get something very basic to work.

What i want is to run a python script that will fish out all the tga files in the folders and move them into the root folder. Just move them one level up. The python script will be on the root folder so you can use it current location?
to move the folders into.

thanks in advance :):

http://docs.python.org/2/library/os.htmlA couple of basics

  1. I would use a command line instead of the location of the script, it’s more flexible if you need to run it from another script
  2. Finding files is easy, you can loop over folder hierarchies with os.walk
  3. If you are copying use shutil.copy

Generically, a script like this will do something along the lines of


import os
import shutil
import sys


# this does the actual work
def move_files_by_pattern (pattern, sourcefolder, targetfolder):
    for path, folders, files in os.walk(sourcefolder):
        print files
        for eachfile in files:
            if pattern in eachfile:
                shutil.move(path + "/" + eachfile, targetfolder)
                print eachfile



# this makes it work from the commandline
if __name__ == '__main__':
    pattern = sys.argv[1]
    sourcefolder = sys.argv[2]
    targetfolder = sys.argv[3]
    move_files_by_pattern (pattern, sourcefolder, targetfolder)


# call something like "c:\path	o\python.exe  move_files_by_pattern.py   tga  path	o\src  path	o	arget

This coves generic find-and-move behavior, it’s easy to add some logic to limit it or customize it once you see how this works.

[QUOTE=Theodox;23775]http://docs.python.org/2/library/os.htmlA couple of basics

  1. I would use a command line instead of the location of the script, it’s more flexible if you need to run it from another script
  2. Finding files is easy, you can loop over folder hierarchies with os.walk
  3. If you are copying use shutil.copy

Generically, a script like this will do something along the lines of


import os
import shutil
import sys


# this does the actual work
def move_files_by_pattern (pattern, sourcefolder, targetfolder):
    for path, folders, files in os.walk(sourcefolder):
        print files
        for eachfile in files:
            if pattern in eachfile:
                shutil.move(path + "/" + eachfile, targetfolder)
                print eachfile



# this makes it work from the commandline
if __name__ == '__main__':
    pattern = sys.argv[1]
    sourcefolder = sys.argv[2]
    targetfolder = sys.argv[3]
    move_files_by_pattern (pattern, sourcefolder, targetfolder)


# call something like "c:\path	o\python.exe  move_files_by_pattern.py   tga  path	o\src  path	o	arget

This coves generic find-and-move behavior, it’s easy to add some logic to limit it or customize it once you see how this works.[/QUOTE]

Thanks “Theodox”
but im really stuck :frowning: i have trying to figure this out for the past 4hours, im very new to python so have no clue.
i think thats easiest way from me to get this work. If i dump the py script file into the root folder then i can use “os.getcwd()” to get the path to:
-1 search into the dir to find tga’s
-2 to move the tga’s into

just looking for a very fast simple solution, sorry just kind of lost with this :frowning:

you should just be able to do it from the command line:


c:\python26\python.exe    c:\here\is\my\script   tga   c:\files\are\here    c:\files\go\here

However you could convert it to what you want by changing the lines under ‘if name == main’:


if __name__ == '__main__':
    pattern = "tga"
    sourcefolder = os.getgetcwd()
    targetfolder = os.path.join(os.getcwd, "..")
    move_files_by_pattern (pattern, sourcefolder, targetfolder)

Remember though that the way you’re setting it up if you have multiple files with the same names in different folders there will be overwrites.