Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将路径名写入文件(Python)_Python_Search_Filehandle - Fatal编程技术网

将路径名写入文件(Python)

将路径名写入文件(Python),python,search,filehandle,Python,Search,Filehandle,我正在处理大量图像,并试图搜索JPEG,然后将其路径写入文件 目前,我可以找到所有我想要的JPEG。但是,我的“索引”文件中的每个新路径都会覆盖最后一个路径。因此,它根本不是一个索引/列表,而是一个包含/path/to/just/one/file.jpg的文本文件 我简化了我的代码,并在下面添加了它。这是冗长的,但这对我的阅读有益,也对像我这样的新手有益 #---------- #-->Import Modules #I'm pretty sure there is redundancy

我正在处理大量图像,并试图搜索JPEG,然后将其路径写入文件

目前,我可以找到所有我想要的JPEG。但是,我的“索引”文件中的每个新路径都会覆盖最后一个路径。因此,它根本不是一个索引/列表,而是一个包含/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_索引文件中以前的任何内容。因为每次在写入jpeg路径之前都会调用此方法,所以只剩下最后一个。使用“a”(append)代替“w”(write),告诉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)
或者,您也可以使用。。。as语句位于for循环之外。这样,jpg_索引文件将只在开始时打开和覆盖一次,而不是在其中已有信息之后

例如:

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)