Solved

Explanation of Python code

Posted on 2007-12-02
7
3,580 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
  • 4
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
Wod earned 190 total points
Comment Utility
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 200 total points
Comment Utility
# 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
Comment Utility
oops, to slow typing
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mgjust
Comment Utility
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 200 total points
Comment Utility
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
Comment Utility
So all this code does is move files from one directory to another?
0
 
LVL 55

Assisted Solution

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I feel like more and more people want to know how to programmatically convert addresses into geospatial locations. So in this article, I will show you how you can do it with Bing Maps. I'm going to use PowerShell, which is a nice scripting language,…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now