Select to view content in your preferred language

Copy personal geodatabases to another directory with Python

662
4
08-23-2010 08:29 AM
SheriNorton
Frequent Contributor
I need to copy a set of personal geodatabases from a master directory to a local working directory for processing. Here's my Python code:

import
os
import shutil

dirlist=os.listdir("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW")

dst="C:\\Arcwork\\parcelmdb"

mdblist=[]
for item in dirlist:
[INDENT]     if item.lower().endswith("mdb"):
        mdblist.append(item)
[/INDENT]  for db in mdblist:
[INDENT]     shutil.copyfile(db,dst)
[/INDENT]However, when run I get an error:
Traceback (most recent call last):
  File "C:\Arcwork\GetMasterMDBList.py", line 11, in <module>
    shutil.copyfile(item,dst)
  File "C:\Python26\ArcGIS10.0\lib\shutil.py", line 52, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: 'Bolton.mdb'

- I seems like the copyfile command has trouble reading the mdblist? I also tried to use the copyfile function in the first For loop (skipping the Append to list), but no luck. Any thoughts on how to intepret this error is great appreciated!
0 Kudos
4 Replies
JasonScheirer
Esri Alum
Try adding the directory name to the source:

mdblist.append(os.path.join("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW", item))
0 Kudos
KarlOrmer
Emerging Contributor
I need to copy a set of personal geodatabases from a master directory to a local working directory for processing. Here's my Python code:

import
os
import shutil

dirlist=os.listdir("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW")

dst="C:\\Arcwork\\parcelmdb"

mdblist=[]
for item in dirlist:
[INDENT]     if item.lower().endswith("mdb"):
        mdblist.append(item)
[/INDENT]  for db in mdblist:
[INDENT]     shutil.copyfile(db,dst)
[/INDENT]However, when run I get an error:
Traceback (most recent call last):
  File "C:\Arcwork\GetMasterMDBList.py", line 11, in <module>
    shutil.copyfile(item,dst)
  File "C:\Python26\ArcGIS10.0\lib\shutil.py", line 52, in copyfile
    fsrc = open(src, 'rb')
IOError: [Errno 2] No such file or directory: 'Bolton.mdb'

Just so that you understand why your approach didn't work: print the arguments (`db` and `dst`) before you pass them to shutil.copyfile. Probably db is 'Bolton.mdb'. If you pass that to shutil.copyfile it guesses './Bolton.mdb' where '.' is the directory the script was started in - and it doesn't seem that there is a file called 'Bolton.mdb'.
0 Kudos
ChrisMathers
Deactivated User
I agree with jscheirer that copyfile is missing the path to the mdb. Also, since you are only moving to one directory, you may as well hard code that into the function instead of using a variable.


for db in mdblist:
[INDENT]shutil.copyfile(os.path.join("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW",db), "C:\\Arcwork\\parcelmdb")
[/INDENT]
0 Kudos
SheriNorton
Frequent Contributor
Here's what worked:

import os
import shutil

dirlist=os.listdir("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW")      #Initial directory with personal geodatabases
dst="C:\\Arcwork\\parcelmdb"       #Destination folder for copies of personal geodatabases

mdblist=[]     #Defines the list to hold the full path and file name of each personal geodatabase
for item in dirlist:     #Loops through all the contents  of the initial diretory
[INDENT]if item.lower().endswith("mdb"):    #Find files with MDB extension
jmdb=os.path.join("Z:\\COMMON\\GIS\\WORK\\Ron\\NEW", item)  #Combine dir path and filename to variable
mdblist.append(jmdb)   #Add variable value (full path and filename) to LIST
shutil.copy(jmdb,dst)    #Copy list item to destination directory
[/INDENT]
0 Kudos