The script below iterates through a folder and determines what version of ArcMap (9.0 - 10.5) each MXD was saved in then writes to a text file. The file is saved with the string in line 11. It's telling me this script also has a syntax error in line 11, but I can not see why? I'm running PythonWin 2.7 on Windows 10.
What I want is to have the file named after the directory (i.e. whatever the user puts in the workspace parameter of the script tool). Is there a way to do that?
import glob, os, sys
from oletools.thirdparty import olefile
import arcpy
arcpy.env.overwriteOutput = True
Workspace = arcpy.GetParameterAsText(0)
mxdFiles = glob.glob(os.path.join(Workspace, '*.mxd')
# Write to textfile
txtFile = open(Workspace + "{}".format("Identify ArcMapVersion.txt"), "w")
txtFile.write("The folling list is the file path(s) to the MXD(s) followed by the ArcMap version" + "\n")
txtFile.write("-------------------------------------------------------------------" + "\n")
def mxd_version(filename):
ofile = olefile.OleFileIO(filename)
stream = ofile.openstream('Version')
data = stream.read().decode('utf-16')
version = data.split('\x00')[1]
return version
for mxdFile in mxdFiles:
fileName = os.path.basename(mxdFile)
print mxdFile, (mxd_version(mxdFile))
arcpy.AddMessage("\t Successful: " + mxdFile + " " + (mxd_version(mxdFile)) + "\n")
txtFile.write("\t Successful: " + mxdFile + " " + (mxd_version(mxdFile)) + "\n")
arcpy.AddMessage("------------------All MXDs Completed-------------------")
txtFile.write("------------------All MXDs Completed--------------------")
txtFile.close()
Solved! Go to Solution.
The open function requires a valid file name(it doesn't have to exist, it just has to be a file, right now you are using a directory as the input, which probably causing the issue.).
If you are needing to use the name of the folder that the txt document is being saved in as the name, I would use os.path.basename to get the name of the folder you are operating in then use that as the name of your text document.
Something like this:
file = os.path.join(Workspace, "{}" .format(os.path.basename(Workspace) + ".txt"))
https://docs.python.org/2/library/os.path.html#os.path.basename
See if the missing ) in line 8 is the issue.
I always look at the preceding line first, if I get a syntax error I can't find in the line it says is in error.
Thanks Rebecca, that was it.
The bigger problem is arranging the script to get it to save with the directory name instead of what I have as the string in line 11-- "Identify ArcMapVersion.txt". When using the script tool I have it set up with only one parameter, the workspace folder. How can I change the script to have the text file saved as the name of the folder where the user is saving?
I've tried changing things around in line 11, but I only got errors as the open() function parameters can't be changed.
To be a bit clearer:
This line...
txtFile = open(Workspace + "{}".format("Mapbook_MXDs.txt"), "w")
...saves as a text file, in a workspace folder named "Townships", as such:
TownshipsMapbook_MXDs
I want to have the text file saved as:
"Mapbook_MXDs"
I'd use a print statement on Workspace + "{}".format("Mapbook_MXDs.txt") to make sure its creating the output you think it should. I think you are getting the wrong output due to the fact you are trying to concatenate your workspace and file name but don't have another backslash seperator to have the folder and file name separate. Why not use os.path.join as you did previously?
@ Ian. Thanks, I understand what you're saying, and I was able to run it with os.path.join() as such:
# Write to textfile
file = os.path.join(Workspace, "{}".format("MXDS.txt"))
print file
txtFile = open(file, "w")
txtFile.write("The folling list is the file path(s) to the MXD(s) followed by the ArcMap version" + "\n")
txtFile.write("-------------------------------------------------------------------" + "\n")
And the script ran fine and saved the text file as "MXDS", as the string has it doing. But now I realize I wasn't really asking for the correct procedure. What I really want it to do is save as the workspace folder name. So, when the user is running the script tool the folder being saved to will be what the text file is named.
I tried a couple things such as:
# Write to textfile
file = os.path.dirname(Workspace)
print file
txtFile = open(file, "w")
txtFile.write("The folling list is the file path(s) to the MXD(s) followed by the ArcMap version" + "\n")
txtFile.write("-------------------------------------------------------------------" + "\n")
and got the following error (line 21 in error is line 4 above):
Tried this:
# Write to textfile
#file = os.path.dirname(Workspace)
#print file
txtFile = open(Workspace, "w")
txtFile.write("The folling list is the file path(s) to the MXD(s) followed by the ArcMap version" + "\n")
txtFile.write("-------------------------------------------------------------------" + "\n")
and got the same error.
Any ideas?
The open function requires a valid file name(it doesn't have to exist, it just has to be a file, right now you are using a directory as the input, which probably causing the issue.).
If you are needing to use the name of the folder that the txt document is being saved in as the name, I would use os.path.basename to get the name of the folder you are operating in then use that as the name of your text document.
Something like this:
file = os.path.join(Workspace, "{}" .format(os.path.basename(Workspace) + ".txt"))
https://docs.python.org/2/library/os.path.html#os.path.basename