将路径名写入文件 (Python)
Write Path Names to a File (Python)
我正在处理大量图像并尝试搜索 jpeg,然后将它们的路径写入文件。
目前,我可以找到所有我想要的jpeg。但是,我的 'index' 文件中的每个新路径都会覆盖最后一个。因此,它根本不是 index/list,而是一个包含 a/path/to/just/one/file.jpg
的文本文件
我简化了代码并将其添加到下面。它很冗长,但这是为了我的阅读利益以及其他像我这样的新手。
#----------
#-->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)
我也尝试过使用更简单的想法;
filehandle.write(path)
还有一个我不完全理解的更复杂的;
filehandle.writelines("%s\n" % path for path in search_location)
然而我所做的一切都以稍微不同的方式失败了。
'w' 选项告诉 open() 方法覆盖之前在 jpg_index 文件中的任何内容。因为每次在写入 jpeg 路径之前调用此方法,所以只剩下最后一个。使用 'a'(追加)代替 'w'(写入)告诉 open() 方法追加到文件而不是每次都覆盖它。
例如:
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)
或者,您可以将 with... as 语句移到 for 循环之外。这样,jpg_index文件只会在开始时打开和覆盖一次,而不是在里面已经有信息之后。
例如:
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)
我正在处理大量图像并尝试搜索 jpeg,然后将它们的路径写入文件。
目前,我可以找到所有我想要的jpeg。但是,我的 'index' 文件中的每个新路径都会覆盖最后一个。因此,它根本不是 index/list,而是一个包含 a/path/to/just/one/file.jpg
的文本文件我简化了代码并将其添加到下面。它很冗长,但这是为了我的阅读利益以及其他像我这样的新手。
#----------
#-->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)
我也尝试过使用更简单的想法;
filehandle.write(path)
还有一个我不完全理解的更复杂的;
filehandle.writelines("%s\n" % path for path in search_location)
然而我所做的一切都以稍微不同的方式失败了。
'w' 选项告诉 open() 方法覆盖之前在 jpg_index 文件中的任何内容。因为每次在写入 jpeg 路径之前调用此方法,所以只剩下最后一个。使用 'a'(追加)代替 'w'(写入)告诉 open() 方法追加到文件而不是每次都覆盖它。
例如:
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)
或者,您可以将 with... as 语句移到 for 循环之外。这样,jpg_index文件只会在开始时打开和覆盖一次,而不是在里面已经有信息之后。
例如:
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)