I'm dealing with a large number of images and am trying to conduct a search for jpegs and then write their paths to a file.
Currently, I can find all the jpegs I want. However, each new path in my 'index' file overwrites the last. Consequently, it's not an index/list at all, but a text file containing a/path/to/just/one/file.jpg
I've simplified my code and added it below. It's verbose but that's for my reading benefit as well as for others who are new like myself.
#----------
#-->Import Modules
#I'm pretty sure there is redundancy here and it's not well laid out
#but I'm new to coding and it works
import os
import pathlib
import glob, os
from pathlib import Path
import os.path
from os import path
#----------
#-->Global Vars
#Simplified example of my variables
working_dir = "/Users/myname/path/to/working dir"
records_dir = str(working_dir + "/Records")
#----------
#-->Search Location
#Define where the jpeg search is to take place
#(actually dictated via user input, Minecraft is just an example)
search_locations = ["/Users/myname/minecraft"]
#---------
#--> Search for jpgs and write their paths to a file
#define the file where the jpeg paths are to be stored,
#(I'm referring to the storage file as an index of sorts)
jpg_index = str(records_dir + "/index_for_all_jpgs")
#Its embedded in a forloop because the user can add multiple locations
for search_location in search_locations:
#get the desired paths from the search location
for path in Path(search_location).rglob('*.jpg'):
#Open the index where paths are to be stored
with open(jpg_index, 'w') as filehandle:
#This is supposed to write each paths as a new line
#But it doesn't work
filehandle.writelines('%s\n' % path)
I have also tried using a simpler idea;
filehandle.write(path)
and a more complex one that I don't fully understand;
filehandle.writelines("%s\n" % path for path in search_location)
Yet all I have done is failed in a slightly different way.
The 'w' option tells the open() method to overwrite anything previously in the jpg_index file. Because you call this method each time before you write a jpeg path to it, only the last one remains. Use 'a' (append) in place of 'w' (write) to tell the open() method to append to the file instead of overwriting it each time.
For instance:
for search_location in search_locations:
for path in Path(search_location).rglob('*.jpg'):
with open(jpg_index, 'a') as filehandle:
filehandle.writelines('%s\n' % path)
Alternatively, you could move the with... as statement outside of the for loops. This way, the jpg_index file will only be opened and overwritten once at the beginning and not after there is already information in it.
For example:
with open(jpg_index, 'w') as filehandle:
for search_location in search_locations:
for path in Path(search_location).rglob('*.jpg'):
filehandle.writelines('%s\n' % path)