<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Scripts only work if in same folderas other files in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275403#M21268</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think your syntax on the join should be:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;s += "\n".join(dir)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:25:41 GMT</pubDate>
    <dc:creator>BruceNielsen</dc:creator>
    <dc:date>2021-12-11T13:25:41Z</dc:date>
    <item>
      <title>Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275394#M21259</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This doesn't happen when running something in ArcMap itself, either a script or python window.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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 &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;looks&lt;/SPAN&gt;&lt;SPAN&gt; ok, V. 2,6, etc, but maybe I missed something, like setting the right path?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway, thanks for any help, and sorry if this isn't really an ArcMap python issue.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 12:46:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275394#M21259</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2012-03-15T12:46:48Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275395#M21260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 12:52:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275395#M21260</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-15T12:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folder as other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275396#M21261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isdir(dir):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write(dir + "\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; s += dir.join("\n")

fileList.write(s + "\n")
&amp;nbsp;&amp;nbsp; 
listing = os.listdir(dirPath)

fileList.write("\n")
fileList.write("FILES:\n")
for file in listing:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isfile(file):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write(file + "\n")

fileList.close()

prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:25:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275396#M21261</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T13:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275397#M21262</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yup, definitely some system or environment setting throwing a wrench in things. What does this return?&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
os.getenv("PYTHON")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:25:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275397#M21262</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T13:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275398#M21263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;fileName = "C:\FileList.txt"&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Don't use backslashes in paths. It escapes the next character.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Instead it should be: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"C:/FileList.txt" &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;OR &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;"C:\\FileList.txt" &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;OR &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;r"C:\FileList.txt"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 14:06:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275398#M21263</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-03-15T14:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275399#M21264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Yup, definitely some system or environment setting throwing a wrench in things. What does this return?&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
os.getenv("PYTHON")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the PythonWin interactive window, that returns NONE when I print it. Nothing happens if I don't use print.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;Don't use backslashes in paths. It escapes the next character.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:25:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275399#M21264</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T13:25:38Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275400#M21265</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;In the PythonWin interactive window[...]&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Well there's your problem right there. Try it in IDLE.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If that doesn't work I would recommend reinstalling Python. Here's a thread with some good tips on repairing broken Python installations.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 16:15:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275400#M21265</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-15T16:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275401#M21266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;IDLE returns NONE as well. It doesn't look like the link to the repairing installs thread you mentioned posted, though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 16:28:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275401#M21266</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2012-03-15T16:28:05Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275402#M21267</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Oh, my mistake&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://forums.arcgis.com/threads/23230-quot-ImportError-No-module-named-arcpy-quot-PYTHON"&gt;http://forums.arcgis.com/threads/23230-quot-ImportError-No-module-named-arcpy-quot-PYTHON&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2012 16:40:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275402#M21267</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-15T16:40:22Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275403#M21268</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think your syntax on the join should be:&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;s += "\n".join(dir)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:25:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275403#M21268</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2021-12-11T13:25:41Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275404#M21269</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Revised script:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isdir(dir):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write(dir + "\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write("Not a directory: " + dir + "\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #s += "\n".join(dir)

#fileList.write(s + "\n")

listing = os.listdir(dirPath)

fileList.write("\n")
fileList.write("FILES:\n")
for file in listing:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isfile(file):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write(file + "\n")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fileList.write("Not a file: " + file + "\n")

fileList.close()

prompt = "File list created at: " + fileName + "\nPress Enter"
ok = raw_input(prompt)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;and output when run from a script not in the folder entered at the prompt (these are the correct files &amp;amp; folders):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
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

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:25:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275404#M21269</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T13:25:43Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275405#M21270</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You might not be reading your dir properly. Try this on some directory. Replace "C:\\test" with yours.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import os testdir = "C:\\test" for dir in os.listdir(testdir):&amp;nbsp; dir = os.path.join(testdir,dir)&amp;nbsp; if os.path.isdir(dir): &amp;nbsp; print dir + " is a directory"&amp;nbsp; elif os.path.isfile(dir): &amp;nbsp; print dir + " is a file"&amp;nbsp; else: &amp;nbsp; print dir + " is not a directory"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Mar 2012 13:51:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275405#M21270</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-03-20T13:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Scripts only work if in same folderas other files</title>
      <link>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275406#M21271</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Success! Thanks Mathew, and everyone else. Not real sure &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;why&lt;/SPAN&gt;&lt;SPAN&gt; 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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, thanks much.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Mar 2012 15:00:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/scripts-only-work-if-in-same-folderas-other-files/m-p/275406#M21271</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2012-03-20T15:00:41Z</dc:date>
    </item>
  </channel>
</rss>

