简体   繁体   English

在python中比较文件和文件夹名称

[英]comparing file and folder name in python

I want to compare the content of a file(.txt) in which there are only IP addresses to the name of folders with are named by IP addresses. 我想比较仅具有IP地址的文件(.txt)的内容与以IP地址命名的文件夹名称。 The file is present in the same directory in which the are folders present, like this 该文件位于存在文件夹的目录中,就像这样

XYZ XYZ

|___ 10.25.85.10 | ___ 10.25.85.10

|___ 10.02.25.11 | ___ 10.02.25.11

|___ server.txt | ___ server.txt

XYZ is the main directory with two are folders and server.txt. XYZ是主目录,其中两个是文件夹和server.txt。 It(server.txt) is the file in which folder names like (10.25.85.10, 10.02.25.11) are already present and one extra assume 10.0.0.0. 该文件(server.txt)是其中已经存在文件夹名称(10.25.85.10,10.02.25.11)的文件,另外一个文件夹的名称为10.0.0.0。 Now I want that extra IP to get saved in a list after comparing file content and folder name. 现在,我希望在比较文件内容和文件夹名称之后,将多余的IP保存在列表中。 How can I solve this? 我该如何解决?

Read the file contents in a list however you like, 根据需要阅读列表中的文件内容,

Then iterate over the list of IPs 然后遍历IP列表

import os
dir_content = os.listdir()
not_in_dir = []
ips = ["10.25.85.10", "10.02.25.11", "10.0.0.0"]
for ip in ips:
     if ip not in dir_content:
             not_in_dir.append(ip)
print(not_in_dir)

This will get you 这会让你

["10.0.0.0"]

Hope this helps you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM