Solved

Explanation of Python code

Posted on 2007-12-02
7
3,588 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
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
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.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python tuple index error 4 86
Query GPS coordinates in MSSQL from QGIS 3 55
Python - how to convert UTC time string to local time 4 151
Python Regex Problem 24 139
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…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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 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…

791 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