如何比较列表中的更新值

问题描述 投票:0回答:2

我有两个目录 - 一个包含较旧的数据,一个包含较新的数据。当文件在目录之间移动时,文件名将从“old-filename.ext”更新为“old-filename-updated.ext”。文件扩展名的长度并不总是相同。我试图弄清楚如何检查新目录以查看是否存在具有正确文件名的更新文件,如果不存在,则创建一个新文件。

    for files in old_dir:
      old_files = [:-5] #longest file extension is 4 chars long
      for file in new_dir:
        if old_files in new_dir:
          lst_updated_files.append(old.files)

这会生成一个列表,其中仅包含新目录中匹配的文件名。但这就是我陷入困境的地方。我将如何获取剩余文件并在更新的目录中创建它们的新版本?

python string filenames
2个回答
0
投票

您可以尝试使用

os.listdir()
https://www.geeksforgeeks.org/python-os-listdir-method/

python中的os.listdir()方法用于获取指定目录下的所有文件和目录的列表。如果我们不指定任何目录,则将返回当前工作目录中的文件和目录列表。

import os old_directory = os.listdir("path/to/old/directory") new_directory = os.listdir("path/to/new/directory")
接下来我们需要比较目录以查看哪些文件尚未移动到新目录。如果我们知道“新”目录中的所有文件都将具有后缀 

-updated

,例如 
old-filename-updated.ext
,那么我们可以从 
-updated
 中的每个文件名字符串中删除 
new_directory
,以便名称与 
中的名称匹配old_directory
,可以更轻松地比较以找到缺少的内容。

import os old_directory = os.listdir("path/to/old/directory") new_directory = os.listdir("path/to/new/directory") # Use list comprehension to modify each element in new directory and remove "-updated" new_directory_modified = [element.replace("-updated", "") for element in new_directory]
现在我们可以使用集合算术找到丢失的文件:

import os old_directory = os.listdir("path/to/old/directory") new_directory = os.listdir("path/to/new/directory") # Use list comprehension to modify each element in new directory and remove "-updated" new_directory_modified = [element.replace("-updated", "") for element in new_directory] # Using `set` is faster than using `in` to find missing elements missing_elements = list(set(old_directory) - set(new_directory_modified))
从这里开始,您需要做的就是迭代此列表,将 

-updated

 添加到每个元素,然后保存到新目录中。


0
投票
您可以执行类似以下操作来比较两个目录中的

文件名。请注意,这不会比较文件内容修改日期。因此,此代码片段不会检查新目录中的文件是否确实是旧目录中文件的更新版本。您可以使用例如检查新目录中的文件是否实际上比旧目录中的相应文件新os.stat

(参见
python:哪个文件更新以及更新了多少时间)。

import os import shutil # Get all files in the old and new directory. # os.listdir returns everything inside the given directory (including other directories). # os.path.isfile checks if the given path is a file. dir_old = "path/to/old/directory" dir_new = "path/to/new/directory" files_old = [file for file in os.listdir(dir_old) if os.path.isfile(file)] files_new = [file for file in os.listdir(dir_new) if os.path.isfile(file)] # Artificial lists for testing purpose. files_old = ["file1.txt", "file2.jpeg", "file3.so", "file4.a"] files_new = ["file1_updated.txt", "file3_updated.so"] files_updated = [] files_not_updated = [] for file_old in files_old: # Split file_old in its basename and its extension. fname, extension = os.path.splitext(file_old) # Assemble the corresponding new file name. file_new = fname + "_updated" + extension if file_new in files_new: # The file has already been updated. files_updated.append(file_new) else: # The file has not been updated, yet. files_not_updated.append(file_new) # Alternative 1: Create an empty file with the updated file name. open(os.path.join(dir_new, file_new), "x").close() # Alternative 2: Copy the old file to the new directory. shutil.copy(file_old, os.path.join(dir_new, file_new) # Or do whatever you do to actually update the file.
    
© www.soinside.com 2019 - 2024. All rights reserved.