?
Solved

Explanation of Python code

Posted on 2007-12-02
7
Medium Priority
?
3,604 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

743 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