Select to view content in your preferred language

Scripts only work if in same folderas other files

1719
12
Jump to solution
03-15-2012 05:46 AM
Zeke
by
Honored Contributor
I'm not sure if this question is directly related to ArcMap, but since that's mostly what I use Python for I'll give it a shot.
When I write a script in Python, it runs fine if it's in the same folder as the file(s), such as the mxd, it references. If the script is in another folder, it doesn't run, at least not correctly.
This happens with scripts for ArcMap, as well as other projects. For example, I have a script to print directories and files in a folder. If the script is in that folder, it runs fine. If it's in another folder, it only prints its own name (a copy is in the other folder, so original is still there).
This doesn't happen when running something in ArcMap itself, either a script or python window.
One last thing: the sysadmin didn't install Python from the ArcMap discs. I manually installed it according to other posts in the forums, and everything looks ok, V. 2,6, etc, but maybe I missed something, like setting the right path?
Anyway, thanks for any help, and sorry if this isn't really an ArcMap python issue.
Tags (2)
0 Kudos
12 Replies
Zeke
by
Honored Contributor
Thanks for all the help. I reinstalled Python per the link in Mathew's suggestion, as well as the other fixes recommended. I modified the script as shown below to check the if statement, and the problem seems to be that os.path.isdir() and isfile() isn't recognizing files and directories (except for itself). It doesn't even recognize the other python script in the folder as a file. Is isdir/isfile deprecated and no longer supported? Even though the script recognizes itself, and runs properly from within the folder. Kind of baffling.

Revised script:
import os

prompt = "Please enter the directory path: "
dirPath = raw_input(prompt)
listing = os.listdir(dirPath)
fileName = r"C:\FileList.txt"
fileList = open(fileName, "w+")
fileList.write(dirPath + "\n\n")

fileList.write("DIRECTORIES:\n")
for dir in listing:
    if os.path.isdir(dir):
        fileList.write(dir + "\n")
    else:
        fileList.write("Not a directory: " + dir + "\n")
        #s += "\n".join(dir)

#fileList.write(s + "\n")

listing = os.listdir(dirPath)

fileList.write("\n")
fileList.write("FILES:\n")
for file in listing:
    if os.path.isfile(file):
         fileList.write(file + "\n")
    else:
        fileList.write("Not a file: " + file + "\n")

fileList.close()

prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)


and output when run from a script not in the folder entered at the prompt (these are the correct files & folders):
C:\Users\gkeith\Desktop\PythonScripts

DIRECTORIES:
Not a directory: abc00000.txt
Not a directory: abc0000x.txt
Not a directory: abc000xx.txt
Not a directory: abc00zzz.txt
Not a directory: abc0xxxx.txt
Not a directory: abcuvwxyz.txt
Not a directory: abcxxxxx.txt
Not a directory: FileLister2.py
Not a directory: New folder
Not a directory: New folder (2)
Not a directory: New folder (3)
Not a directory: RenamePDF.py

FILES:
Not a file: abc00000.txt
Not a file: abc0000x.txt
Not a file: abc000xx.txt
Not a file: abc00zzz.txt
Not a file: abc0xxxx.txt
Not a file: abcuvwxyz.txt
Not a file: abcxxxxx.txt
FileLister2.py
Not a file: New folder
Not a file: New folder (2)
Not a file: New folder (3)
Not a file: RenamePDF.py

0 Kudos
MathewCoyle
Honored Contributor
You might not be reading your dir properly. Try this on some directory. Replace "C:\\test" with yours.
import os testdir = "C:\\test" for dir in os.listdir(testdir):  dir = os.path.join(testdir,dir)  if os.path.isdir(dir):   print dir + " is a directory"  elif os.path.isfile(dir):   print dir + " is a file"  else:   print dir + " is not a directory"
0 Kudos
Zeke
by
Honored Contributor
Success! Thanks Mathew, and everyone else. Not real sure why that worked for outside the same folder while the original worked, only, in the same folder, but that's ok. I though i took the isdir() syntax directly from documentation somewhere, but who knows?
Also switched to PyScripter instead of PythonWin. Doubt it made a difference in the code, but is a much nicer scripting environment. Now I'll try it with some arcpy imports.
Again, thanks much.
0 Kudos