?
Solved

Explanation of Python code

Posted on 2007-12-02
7
Medium Priority
?
3,609 Views
Last Modified: 2012-06-27
Hello,
I am very new to python and am trying to understand the below code to possibly chop it up and use it to my advantage it used in ESRI ArcMAP 9.1
Would someone please annotate the below code:

import os, os.path, shutil
dir1 = "c:/temp2"
dir2 = "c:/temp3"

fileList = os.listdir(dir1)

for inFile in fileList:
  aFile = os.path.basename(inFile)
  if aFile <> "":
    inFile  = str(dir1 + "/" + inFile)
    outFile = str(dir2 + "/" + aFile)
    print "Input file:  " + inFile
    print "Output file: " + outFile
    try:
      shutil.copyfile(inFile, outFile)
    except:
      print "Unable to copy " + inFile + " to " + outFile

Thank you kindly,
0
Comment
Question by:mgjust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
Wod earned 760 total points
ID: 20392321
It copies all files from dir1 to dir2

import os, os.path, shutil
dir1 = "c:/temp2"                            # copy all files from this dir,
dir2 = "c:/temp3"                            # to this one

fileList = os.listdir(dir1)                  # get list of files to copy

for inFile in fileList:                         # loop through the file names
  aFile = os.path.basename(inFile) # extract filename from path
  if aFile <> "":                                # unless filename is empty
    inFile  = str(dir1 + "/" + inFile)    # append filename to dir1
    outFile = str(dir2 + "/" + aFile)   # append filename to dir2
    print "Input file:  " + inFile          # text output
    print "Output file: " + outFile      # text output
    try:                                              # if the try fails, it jumps to except
      shutil.copyfile(inFile, outFile)     # copy the file
    except:                                        # there was an error
      print "Unable to copy " + inFile + " to " + outFile
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 800 total points
ID: 20392336
# required libraries
import os, os.path, shutil
# some source folder
dir1 = "c:/temp2"
# some destination folder
dir2 = "c:/temp3"

# extract the list of all files in source folder
fileList = os.listdir(dir1)

# for each file in folder
for inFile in fileList:
# extract the filename without path
  aFile = os.path.basename(inFile)
# if there is a filename (if it is not a subfolder)
  if aFile <> "":
# set the source pathname (really equal to 'inFile')
    inFile  = str(dir1 + "/" + inFile)
# set the destination pathname, with the same filename
    outFile = str(dir2 + "/" + aFile)
# print source pathname
    print "Input file:  " + inFile
# print destination pathname
    print "Output file: " + outFile
# copy source to destination file
    try:
      shutil.copyfile(inFile, outFile)
# if an error, show error message
    except:
      print "Unable to copy " + inFile + " to " + outFile

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20392338
oops, to slow typing
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mgjust
ID: 20392343
Thanks for the quick reply:
But in addition to copying files it also changes their names, right?

For example:
If the filename in dir1 was test123, what the file name in dir2 be?

Thanks again
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 800 total points
ID: 20392354
no, it doesn't  change it.
but I think this line is not ok:

# set the source pathname (really equal to 'inFile')
    inFile  = str(dir1 + "/" + inFile)

it is not necessary.
0
 

Author Comment

by:mgjust
ID: 20392380
So all this code does is move files from one directory to another?
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 800 total points
ID: 20392386
yes, it moves ALL the files of one directory to another. It doesn't  move subfolder contents
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question