简体   繁体   中英

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. The file is present in the same directory in which the are folders present, like this

XYZ

|___ 10.25.85.10

|___ 10.02.25.11

|___ server.txt

XYZ is the main directory with two are folders and 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. Now I want that extra IP to get saved in a list after comparing file content and folder name. How can I solve this?

Read the file contents in a list however you like,

Then iterate over the list of IPs

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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