Skip to content

Instantly share code, notes, and snippets.

@atc0005
Last active October 19, 2023 19:07
Show Gist options
  • Save atc0005/51cc671b02990f2c9fbfd772d87f7124 to your computer and use it in GitHub Desktop.
Save atc0005/51cc671b02990f2c9fbfd772d87f7124 to your computer and use it in GitHub Desktop.
Python & Go | Check if lines in one file are in another file
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
var userLogins int
usersFile, err := os.Open("users_list.txt")
if err != nil {
panic(err)
}
defer usersFile.Close()
loginsFile, err := os.Open("logins.txt")
if err != nil {
panic(err)
}
defer loginsFile.Close()
usersScanner := bufio.NewScanner(usersFile)
loginsScanner := bufio.NewScanner(loginsFile)
for usersScanner.Scan() {
user := strings.ToLower(usersScanner.Text())
for loginsScanner.Scan() {
login := strings.ToLower(loginsScanner.Text())
if user == login {
userLogins++
}
}
if err := loginsScanner.Err(); err != nil {
panic(err)
}
}
if err := usersScanner.Err(); err != nil {
panic(err)
}
fmt.Printf("%d matching user logins\n", userLogins)
}
users_fh = open("users_list.txt")
logins_fh = open("logins.txt")
matching_user_logins = 0
for user in users_fh:
user = user.strip().lower()
# https://stackoverflow.com/questions/34553559/python-nested-loops-only-working-on-the-first-pass
logins_fh.seek(0)
for login in logins_fh:
login = login.strip().lower()
if user == login:
matching_user_logins += 1
users_fh.close()
logins_fh.close()
print("{} matching user logins".format(matching_user_logins))
@atc0005
Copy link
Author

atc0005 commented May 6, 2021

Added Go code as a separate "file" to this Gist. I'm thinking I'd be better served by setting up a small repo for this work.

@atc0005
Copy link
Author

atc0005 commented Oct 19, 2023

Added Go code as a separate "file" to this Gist. I'm thinking I'd be better served by setting up a small repo for this work.

I spun off local test code to prototype this.

Note to self: Look in the check-if-lines-in-file-are-in-another-file branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment