在2个不同目录中比较多个文件名

发布于 2025-02-01 18:23:53 字数 826 浏览 4 评论 0原文

我有2个文件夹(EX1,EX2),其中文件夹EX1包含2500个文件和文件夹EX2包含2300个文件。文件夹中的某些文件ex2中包含与文件夹EX1相同的内容,我正在尝试:

  • 比较两个目录中的文件标题,
  • 从两个目录中比较每个单词的内容
  • 。两个目录。

如果以上条件匹配,则匹配的文件将以“复制”的前缀重命名,然后在文件夹EX2中的文件名中重命名。

如果条件不匹配,则将文件移至文件夹EX2,并重命名为前缀“ new”,然后使用文件名。

到目前为止,我已经编写了代码以匹配两个目录中的单个文件名。但是,在进行多个研究并纠正语法后,我无法执行该程序。

我认为该方法无效解决此问题。有人会很友善地提出更好的方法并提供帮助解决这个问题的帮助吗?

## Code for files in example1
path = "C:/Users/OneDrive/Example1"
dir_list = os.listdir(path)
print(dir_list)

## code for files in example2
path2 = "C:/Users/OneDrive/Example2"
dir_list2 = os.listdir(path)
print(dir_list2)

comp2=None

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if comp2 = [filecmp.cmp(f,h, shallow= true)] is True:
            print(comp2)

I have 2 folders (Ex1, Ex2), where folder Ex1 contains 2500 files and folder Ex2 contains 2300 files. Some of the files in folder Ex2 contains same content as of folder Ex1, I am trying to:

  • Compare the title of files in both the directories
  • Compare the content from each individual file from both directories
  • Compare the size[in bytes] of individual file in both directories.

If the above condition matches, the matched files are to be renamed with prefix "copied" followed by the file name in folder Ex2.

If the conditions do not match, the files are to be moved to folder Ex2 and renamed with prefix "new" followed by the file name.

So far, I have written the code to match individual file names in both directories. However, upon doing several research and correct syntax, I am failing to execute the program.

I assume the approach is not valid to solve this problem. Would anyone be kind enough to suggest a better approach and provide assistance to solve this problem?

## Code for files in example1
path = "C:/Users/OneDrive/Example1"
dir_list = os.listdir(path)
print(dir_list)

## code for files in example2
path2 = "C:/Users/OneDrive/Example2"
dir_list2 = os.listdir(path)
print(dir_list2)

comp2=None

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if comp2 = [filecmp.cmp(f,h, shallow= true)] is True:
            print(comp2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2025-02-08 18:23:53

这是有效的语法,可以在代码的那部分中执行您认为您的意图。它利用 sissigonment Expression 在Python 3.8中。

...

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if (comp2 := filecmp.cmp(f, h, shallow=True)):
            print(comp2)

Here's valid syntax that does what I think you intended in that section of the code. It makes use of an assignment expression, something that was introduced in Python 3.8.

...

#Iterate through the files in both the folder
for f in dir_list:
    for h in dir_list2:
        if (comp2 := filecmp.cmp(f, h, shallow=True)):
            print(comp2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文