I need to create some files to hyperlink to from Arcmap. I have a folder of jpeg files (100.jpg, 101.jpg etc...). I need to loop through them and create a text file of the same name except replace the *.txt at the end with *.fsv (100.fsv, 101.fsv etc...). This text file of the same name (with the txt replaced with fsv) is a set of instructions on how to open that jpeg in a piece of software so I need to write some instruction in it for example 100.fsv would contain:

open=100.jpg set to 100

everything in the txt file is static except for the name of the jpg it opens. Thanks for any help.

closed as off topic by blah238, RyanDalton, Fezter, dassouki, Devdatta Tengshe May 15 '13 at 3:33

Questions on Geographic Information Systems Stack Exchange are expected to relate to geographic information systems within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 7
    Since this question really only covers the issue of looping through a folder and writing some basic text, and does not have a specific GIS component, it might be better suited for Stack Overflow. – bluefoot May 14 '13 at 20:09
  • 1
    Come on. I know somebody here knows. I am using it for a python script that uses Arcmap tools extensively. – gomapping May 14 '13 at 20:10

This is one way to go about it:

This python snippet will loop over a directory, get all of the filenames that end with a .jpg extension and the create a new text file with the new .fsv extension - this file will contain whatever text you choose to write to it.

import glob
import os
os.chdir("\dir") #the directory containing your .jpegs
    for file in glob.glob("*.jpg"): #iterates over all files in the directory ending in .jpg        
        f = open(( file.rsplit( ".", 1 )[ 0 ] ) + ".fsv", "w") #creates a new file using the .jpg filename, but with the .fsv extension
        f.write('whatever you want in the text file') #write to the text file

You will end up with a new .fsv file for every .jpg in the original folder. The above code will just write all of the new files to the same folder that the .jpgs are stored in. This is by no means beautiful code (no error checking), but it's simple and it works.

  • Thank you for answering my blatantly off topic question. Appreciated. – gomapping May 14 '13 at 20:57
  • @J-roc, you seem to have taken my comment personally. Nobody was getting on you, I was just pointing out the fact that your question doesn't fit the purpose of this specific forum and offering an alternative as a suggestion. You may want to check out the faq since you appear to be relatively new here. – bluefoot May 14 '13 at 21:09
  • No offense taken, I knew I was off topic and really do appreciate someone humouring me. – gomapping May 14 '13 at 21:10

Just to add on to the other answers, you can avoid having to explicitly call the file object's close() method if you use a with statement (which automatically calls close() even if an exception is raised):

with open(filename, 'w') as f:
    f.write("open=" + jpg + " set to 100")

Also don't forget to write a line-ending if that is the intention...

  • Excellent point - less code is usually better. – Radar May 14 '13 at 21:17
  • Fantastic! Totally forgot about the with statement. Very handy. – Mintx May 14 '13 at 21:22

Because you're asking in GIS, I'm including arcpy in my answer. ;)

import arcpy
from arcpy import env

env.workspace = r'C:\your\path\here'

jpgList = arcpy.ListFiles("*.jpg")
for jpg in jpgList:
    jpgnum = jpg[:-4]
    filename = jpgnum + '.fsv'
    f = open(filename, 'w')     
    f.write("open=" + jpg + " set to 100")     

And that's it!

  • Aww, Radar beat me to it. – Mintx May 14 '13 at 21:09
  • 1
    Upvotes for everyone! I feel this answer is just as useful as mine since it offers an alternate solution with more of a direct GIS focus. – Radar May 14 '13 at 21:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.