Select to view content in your preferred language

Scripts only work if in same folderas other files

1704
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
1 Solution

Accepted Solutions
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"

View solution in original post

0 Kudos
12 Replies
MathewCoyle
Honored Contributor
If you post some of the code you are having issues with that may assist in debugging. A manual python install shouldn't really be an issue as long as it is referenced in your environment variables properly. IIRC, you can even have a newer version of python (2.6.5-2.6.x) installed instead of the 2.6.5 that comes with ArcGIS, and that should be fine too.
0 Kudos
Zeke
by
Honored Contributor
I don't think it's a problem with the code itself, since it runs fine when in the same directory as the other files or in ArcMap, but here's an example. Just the last one I worked on, the file listing one I mentioned. One thing that may help - getcwd() always seems to reference the python install path, not whatever path I'm actually working in. It may be a system or environment variable issue.
import os

prompt = "Please enter the directory path: "
dirPath = raw_input(prompt)
listing = os.listdir(dirPath)
fileName = "C:\FileList.txt"
s = "??"
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")
        s += dir.join("\n")

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")

fileList.close()

prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)
0 Kudos
MathewCoyle
Honored Contributor
Yup, definitely some system or environment setting throwing a wrench in things. What does this return?
import os
os.getenv("PYTHON")
0 Kudos
DarrenWiens2
MVP Alum
fileName = "C:\FileList.txt"

Don't use backslashes in paths. It escapes the next character.

Instead it should be:
"C:/FileList.txt"
OR
"C:\\FileList.txt"
OR
r"C:\FileList.txt"
0 Kudos
Zeke
by
Honored Contributor
Yup, definitely some system or environment setting throwing a wrench in things. What does this return?
import os
os.getenv("PYTHON")


In the PythonWin interactive window, that returns NONE when I print it. Nothing happens if I don't use print.


Don't use backslashes in paths. It escapes the next character.


That one I actually know and just forgot, thanks. Still, FileList.txt gets created, it just doesn't contain the list of files. "DIRECTORIES", "??" and "FILES" do print to the file.

Thanks.
0 Kudos
MathewCoyle
Honored Contributor
In the PythonWin interactive window[...]

Well there's your problem right there. Try it in IDLE.

If that doesn't work I would recommend reinstalling Python. Here's a thread with some good tips on repairing broken Python installations.
0 Kudos
Zeke
by
Honored Contributor
IDLE returns NONE as well. It doesn't look like the link to the repairing installs thread you mentioned posted, though.
0 Kudos
MathewCoyle
Honored Contributor
0 Kudos
BruceNielsen
Frequent Contributor
I think your syntax on the join should be:
s += "\n".join(dir)
0 Kudos