Solved

Explanation of Python code

Posted on 2007-12-02
7
3,599 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 190 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 200 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 200 total points
ID: 20392386
yes, it moves ALL the files of one directory to another. It doesn't  move subfolder contents
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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…
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…

687 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