1 Replies - 234 Views - Last Post: 03 July 2013 - 08:16 AM Rate Topic: -----

#1 Riv3r  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 29-January 10

Moving files, but not folders

Posted 03 July 2013 - 07:51 AM

Greetings,

I am in the process of teaching myself to use Python as it is a commonly used language here at my internship/job.

What I am trying to do is move all the files within two directories into a combined folder, but i only want the files themselves not the folder structure. Below is the code I have so far, its nothing remarkable, but I feel as if I am perhaps just missing a useful library and/or method.
i.e:
ResultFail & ResultPass -> Results


I greatly appreciate your time and help

-Riv3r

#Python Script to gather Results files
#Author Riv3r

import os
import shutil
import platform

def toString(tmp):
    if(tmp == CCMA):
        #print("at A")
        return "CCMA"
    elif(tmp == CCMB):
        #print("at B")
        return "CCMB"
    else:
       #print("at C")
        return "CCMC"

#capture login of the machine
usr = os.getlogin().upper()

#List of each individual machine by CCMx
CCMA = ["card1", "card2", "card3"  , "card4"    , "card5",]
CCMB = ["card6", "card7", "card8", "card9", "card10",]
CCMC = ["card11", "card12", "card13", "card14", "card15",]

#List of every machine in test group
labMachines = [CCMA, CCMB, CCMC]
filepath = []

for CCMx in labMachines:
    #print(toString(CCMx))
    for name in CCMx:
        #print(name)
        if(name == usr):
           break
    platform = CCMx
    break

#print(toString(platform))

srcFolder = "C:\login_name\TRs"
dstFolder =  "::file location::" + toString(platform)

tmp = 0
while(tmp < 2):
    try:
        for file in os.listdir(srcFolder):
            #filepath = os.path.join(srcFolder, file) #not being used anymore
            shutil.move(srcFolder, dstFolder)
    except WindowsError:
        print("Error!")
        # can't copy file access times on Windows
        pass
    tmp = tmp + 1

print("Execution Finished!")


This post has been edited by Riv3r: 03 July 2013 - 07:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Moving files, but not folders

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,096
  • Joined: 19-March 11

Re: Moving files, but not folders

Posted 03 July 2013 - 08:16 AM

os.listdir gets you all of the file-like objects, which includes files and directories. You can use a filter or a comprehension, which ever you prefer, to remove directories from that list*, using os.path.isdir() or os.path.isfile(), which return true and false, respectively, if the file descriptor supplied to them is a directory or a file, and vice versa the other way around.

EDIT: *or you could say "to make a list of just the files appearing in that list" if you want

This post has been edited by jon.kiparsky: 03 July 2013 - 08:52 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1